Geeks With Blogs

News


Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done
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;
        }
    }
}
 
 
Posted on Thursday, October 23, 2008 2:45 PM ObjectDataSource | Back to top

Related Posts on Geeks With Blogs Matching Categories

Comments on this post: simulate: “file Streams into Memory, then JOIN the data in Memory, via LINQ to Objects”

# re: simulate: “file Streams into Memory, then JOIN the data in Memory, via LINQ to Objects”
Requesting Gravatar...
Really great post, this was very helpful.
Left by canvas art on Nov 25, 2011 3:30 AM

Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net