473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Linq and creating queries on the fly

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, boolfilter) {
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, boolwith Expression<Func<Person, bool>>
causes a compile error.

Kind regards,
Bram Fokke

Jan 9 '08 #1
2 3032
Bram <b.*****@gmail.comwrote:
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, boolfilter) {
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, boolwith 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.

--
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
Jan 9 '08 #2
Thanks for your swift reply! It works like a charm!

Kind regards
Bram
On Jan 9, 8:08 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Bram <b.fo...@gmail.comwrote:
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, boolfilter) {
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, boolwith 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.

--
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
Jan 9 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ronald S. Cook | last post by:
I've always been taught that stored procedures are better than writing SQL in client code for a number of reasons: - runs faster as is compiled and lives on the database server - is the more...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
2
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
I want to develop a datagridview allowing column selection and filtering, like this: http://www.codeproject.com/KB/grid/GridFilter.aspx This example is using dataset. My question is if: a) I...
3
by: Marc S | last post by:
I have a LINQ class that I can use to query data from my database. I want to query the data directly into an XML string without defining each field. If I have a table Names like the following: ...
7
by: Andy B | last post by:
Just wondering why linq is more useful than datasets? The stuff I do doesn't seem to be too complicated to use linq with it. If I did use linq with it now, I would be doing almost the exact same...
4
by: Jacek Jurkowski | last post by:
Why is it so slow? I really like that queries but using DataReader i have done my task's much more faster than ising LINQ...
6
by: Paulo | last post by:
Why LINQ should be used on my asp.net applications? I use C# 2.0, will I have to move to 3.0/3.5 just because LINQ? Tell me how to convert 2.0 code to LINQ, please. I just want to stay up to...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.