Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi guys.

 

Am playing around with LinQ here, using VS2008Express. And I have this:

 

private void QueryData()
       {
           var peopleOlderThan = from p in people
                                 where p.Birthday < DateTime.Parse(textBox1.Text)
                                 orderby p.Name ascending
                                 select p;

           dataGridView1.DataSource = new BindingSource(peopleOlderThan, null);
        }

 

don't mind the part with the combobox. This statement basically gets data from here:

 

people = new List<Person>();
           people.Add(new Person("Timmy", DateTime.Parse("1/2/1990")));
           people.Add(new Person("Gerald", DateTime.Parse("2/9/1979")));
           people.Add(new Person("Brian", DateTime.Parse("12/29/1973")));

 

This works ok, but I was just wondering if there is a way to expose peopleOlderThan outside of the method? Right now, what I do is put it in a function and return it as IEnumerable.

 

private IEnumerable getList()
       { 
       var peopleOlderThan = from p in people
                                 where p.Birthday < DateTime.Parse(textBox1.Text)
                                 orderby p.Name ascending
                                 select p;
       return peopleOlderThan;
       }

 

all inputs are welcome.

  • Administrators
Posted

You could try something like

IEnumerable peopleOlderThan;

private void QueryData()
{
peopleOlderThan = from p in people
   where p.Birthday < DateTime.Parse(textBox1.Text)
   orderby p.Name ascending
   select p;

dataGridView1.DataSource = new BindingSource(peopleOlderThan, null);
}

 

You should then be able to access peopleOlderThan in other methods etc. by using a generic IEnumerable at least you also get proper intellisense throughout your code as well.

 

If you are messing with multiple threads then the above code is probably a disaster waiting to happen unless you implement some proper synchronisation .

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

I did this:

 

public ReadOnlyCollection<Person> GetPersons 
{ 
get { return peopleOlderThan.AsReadOnly(); } 
} 

 

and am testing right now. Yes PD, the method you posted does supply intellisense but I heeded you warning and didn't use it. I am creating a "faux" data layer so I won't be sure how it will be consumed by other devs so I really want to be on the safe side of this. Thanks soooo much!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...