Thursday, October 23, 2008

simulate: “file Streams into Memory, then JOIN the data in Memory, via LINQ to Objects”
 
I wrote up a quick little WinForms project in VS2008 with .NET Framework 3.5,
 
 
 
 
 
Code overview: 
 
1.    On Form_Load,
a.    create a List of People into DataDridView and in Memory
b.    create a List of Jobs into DataDridView and in Memory
c.    create a List of JobIds into ComboBox
2.    On ComboBox_SelectedIndexChanged,
a.    Perform LINQ to Objects query: “Select from People JOINED to Jobs by JobId”
b.    Show query result in list box
 
You can probably do this with nHibernate too.
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace FascetLINQtoObjects
{
    publicpartialclassForm1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        //-----------------------------------------------------
        //Form Scope (To hold & share LINQ Objects' state) -
        //-----------------------------------------------------
        List<Person> people = newList<Person>();
        List<Job> jobs = newList<Job>();
        bool _loaded = false;
       
        privatevoid Form1_Load(object sender, EventArgs e)
        {
            //-----------------------------------------------------
            //Form Load Load LINQ Objects' state
            //Simulate Fascet Client data streaming into Fascet Winform
            //-----------------------------------------------------
            people.Add(newPerson("Gerald", DateTime.Parse("1/2/2003"), 3));
            people.Add(newPerson("Linda", DateTime.Parse("2/2/2002"), 1));
            people.Add(newPerson("Sarah", DateTime.Parse("4/2/2002"), 1));
            people.Add(newPerson("Bill", DateTime.Parse("4/5/2006"), 2));
 
 
            jobs.Add(newJob(1, "Developer", "Microsoft", "West"));
            jobs.Add(newJob(2, "Developer", "Microsoft", "East"));
            jobs.Add(newJob(3, "Technical Account Representative", "Microsoft", "Central"));
           
            //-----------------------------------------------------
            //Bind to Jobs DataGridView
            //-----------------------------------------------------
            dgPeople.DataSource = people;
            this.dgJobs.DataSource = jobs;
 
            //-----------------------------------------------------
            //Bind to Jobs comboBox
            //-----------------------------------------------------
            this.cboJob.DataSource = jobs;
            this.cboJob.ValueMember = "ID";
            this.cboJob.DisplayMember = "ID";
 
            //-----------------------------------------------------
            //ShowPersonJob
            //-----------------------------------------------------
            int jobId = int.Parse(cboJob.SelectedValue.ToString());
            ShowPersonJob(jobId);
 
            _loaded = true;
 
        }
        privatevoid ShowPersonJob(int jobId)
        {
            //-----------------------------------------------------
            //query joined LINQ objects
            //-----------------------------------------------------
            var result = from p in people
                         from j in jobs
                         where p.JobID == j.ID
                         && j.ID == jobId
                         selectnew
                         {
                             p.Name,
                             j.Company,
                             j.Title
                         };
 
            //-----------------------------------------------------
            //display query result
            //-----------------------------------------------------
            this.listBox1.DataSource = result.ToList();
        }
 
        privatevoid cboJob_SelectedIndexChanged(object sender, EventArgs e)
        {
            //-----------------------------------------------------
            //ShowPersonJob
            //-----------------------------------------------------
            if (_loaded)
            {
                //-----------------------------------------------------
                //
                //-----------------------------------------------------
                int jobId = int.Parse(cboJob.SelectedValue.ToString());
                ShowPersonJob(jobId);
            }
        }
   
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace FascetLINQtoObjects
{
    classPerson
    {
        string _name = "";
        publicstring Name
        {
            get { return _name; }
            set { _name = value; }
        }
        DateTime? _Birthday;
        publicDateTime? Birthday
        {
            get { return _Birthday; }
            set { _Birthday = value; }
        }
        int _jobID = 0;
        publicint JobID
        {
            get { return _jobID; }
            set { _jobID = value; }
        }
        public Person(string name, DateTime birthday, int jobID)
        {
            this._name = name;
            this._Birthday = birthday;
            this._jobID = jobID;
 
        }
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace FascetLINQtoObjects
{
    classJob
    {
        string _title = "";
        publicstring Title
        {
            get { return _title; }
            set { _title = value; }
        }
 
        string _company = "";
        publicstring Company
        {
            get { return _company; }
            set { _company = value; }
        }
 
        int _id = 0;
        publicint ID
        {
            get { return _id; }
            set { _id = value; }
        }
 
        string _location = "";
        publicstring Location
        {
            get { return _location; }
            set { _location = value; }
        }
        public Job(int id, string title, string company, string location)
        {
            _title = title;
            _id = id;
            _company = company;
            _location = location;
        }
    }
}
 
 
 

List All Primary Keys and Foreign Keys

Original article:  http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=41896

set nocount on
create table #PK(constraint_schema sysname not null, constraint_name sysname not null, sql varchar(4000) not null, constraint PK_#PK primary key clustered(constraint_schema, constraint_name))
create table #cols(constraint_schema sysname not null, constraint_name sysname not null, column_name sysname not null, ordinal_position int not null, constraint PK_#PKcol primary key clustered(constraint_schema, constraint_name, ordinal_position))
create table #FK(constraint_schema sysname not null, constraint_name sysname not null,
unique_constraint_schema sysname not null, unique_constraint_name sysname not null,
sql varchar(4000) not null, constraint PK_#FK primary key clustered(constraint_schema, constraint_name))

insert into #PK
select constraint_schema, constraint_name, 'ALTER TABLE ' + quotename(table_schema) + '.' + quotename(TABLE_NAME) +
' ADD CONSTRAINT ' + quotename(CONSTRAINT_NAME) +
' PRIMARY KEY ' + CASE WHEN si.indid<>1 THEN 'NON' ELSE '' END +
'CLUSTERED (>cols<) WITH FILLFACTOR=' + cast(si.OrigFillFactor as varchar) + ' ON ' + quotename(fg.groupname)
AS SQL
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
INNER JOIN sysindexes si on TC.CONSTRAINT_NAME=si.name
inner join sysfilegroups fg on si.groupid=fg.groupid
WHERE CONSTRAINT_TYPE IN('PRIMARY KEY','UNIQUE')

insert into #fk
select c.constraint_schema, c.constraint_name, c.unique_constraint_schema, c.unique_constraint_name,
'ALTER TABLE ' + quotename(F.table_schema) + '.' + quotename(F.table_name) +
' ADD CONSTRAINT ' + quotename(F.constraint_name) +
' FOREIGN KEY(>cols<) REFERENCES ' + quotename(r.table_schema) + '.' + quotename(r.table_name) +
'(>rcols<)'
AS sql
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS F
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C ON F.constraint_schema=C.constraint_schema AND f.constraint_name=c.constraint_name AND F.constraint_type='FOREIGN KEY'
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS R ON R.constraint_schema=C.unique_constraint_schema AND r.constraint_name=c.unique_constraint_name AND r.constraint_type in ('PRIMARY KEY','UNIQUE')
ORDER BY F.table_name, r.table_name

insert into #cols
select constraint_schema, constraint_name, COLUMN_NAME, ORDINAL_POSITION from INFORMATION_SCHEMA.KEY_COLUMN_USAGE

declare @ctr int, @max int, @delim varchar(1)
select @ctr=1, @max=max(ordinal_position), @delim='' from #cols

set nocount on
while @ctr<=@max
BEGIN

update P SET SQL=Replace(SQL, '>cols<', @delim + quotename(c.column_name) + '>cols<')
FROM #PK P INNER JOIN #cols C ON P.constraint_schema=C.constraint_schema AND P.constraint_name=C.constraint_name
WHERE C.ORDINAL_POSITION=@ctr

UPDATE F SET SQL=Replace(Replace(SQL, '>cols<', @delim + quotename(c.column_name) + '>cols<'), '>rcols<', @delim + quotename(r.column_name) + '>rcols<')
FROM #FK F INNER JOIN #cols C ON F.constraint_schema=C.constraint_schema AND F.constraint_name=C.constraint_name AND C.ordinal_position=@ctr
INNER JOIN #cols R ON F.unique_constraint_schema=R.constraint_schema AND F.unique_constraint_name=R.constraint_name AND C.ordinal_position=R.ordinal_position

select @ctr=@ctr+1, @delim=','
END
set nocount on

update #PK SET SQL=Replace(SQL, '>cols<', '')
update #FK SET SQL=Replace(Replace(SQL, '>cols<', ''), '>rcols<', '')

select sql from #PK order by sql
select sql from #FK order by sql

drop table #pk
drop table #fk
drop table #cols