Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Linq and creating queries on the fly

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bram - 09 Jan 2008 18:51 GMT
One of the great things about SQL is that it's exceedingly easy to
create queries on the fly. I'm trying to accomplish the same using
linq but I'm running into problems when I'm trying to do the
following:

void PopulateOldPeople() {
   Populate(p => p.BirthDate < DateTime.Now.AddYears(-65));
}

void PopulateWomen() {
   Populate(p => p.Sex = Sex.Woman);
}

void Populate(Func<Person, bool> filter) {
   var people = from Person p in DB.Persons
                     where filter(p)
                     select p;
   foreach(Person p in people) {
       // Add item to list box
   }
}

This code throws an exception in the foreach loop, when the expression
in 'people' is actually expanded. I'm pretty certain that the reason
is the 'where' clause expects an Expression<Func<T0, R>> and not a
Func<T0, R>. How do I plug an expression into the where clause?
Replacing Func<Person, bool> with Expression<Func<Person, bool>>
causes a compile error.

Kind regards,
Bram Fokke
Jon Skeet [C# MVP] - 09 Jan 2008 19:08 GMT
> One of the great things about SQL is that it's exceedingly easy to
> create queries on the fly. I'm trying to accomplish the same using
[quoted text clipped - 24 lines]
> Replacing Func<Person, bool> with Expression<Func<Person, bool>>
> causes a compile error.

Try this code instead:

void PopulateOldPeople()
{
   Populate(p => p.BirthDate < DateTime.Now.AddYears(-65));
}

void PopulateWomen()
{
   Populate(p => p.Sex = Sex.Woman);
}

void Populate(Expression<Func<Person, bool>> filter)
{
   var people = DB.Persons.Where(filter);

   foreach(Person p in people)
   {
       // Add item to list box
   }
}

Query expressions are nice, but they're not really appropriate in this
case. Just calling the extension method manually is simpler here.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Bram - 09 Jan 2008 20:06 GMT
Thanks for your swift reply! It works like a charm!

Kind regards
Bram

> > One of the great things about SQL is that it's exceedingly easy to
> > create queries on the fly. I'm trying to accomplish the same using
[quoted text clipped - 56 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.