tags:

views:

212

answers:

3

Starting with this code:

int Count(Func<MyClass, bool> conditions)
{
    // ...
}

And I want to call it like so:

int c = Count(foo => foo.bar == 5 && !foo.blah);

How do I then write the count function so that it ends up like so:

int Count(Func<MyClass, bool> conditions)
{
    // what goes here so that I get this:
    string sql = "SELECT COUNT(*) FROM [MyClass] WHERE [Bar] = 5 AND [Blah] = 0"
    // ... execute the sql etc.
}

For the purposes of this discussion, assume that I have already considered and rejected using LINQ to SQL or LINQ to Entities. Not looking for a pre-existing solution but an understanding of how this is done.

+3  A: 

I think what you are looking for is something like this. These are a series of articles that explain how to build a LINQ provider from scratch.

Andrew Hare
Exactly what I needed, thanks!
Nathan Ridley
A: 

Your best bet is probably the series of posts on Matt Warren's blog about building a Custom IQueryable provider.

Samuel Jack
A: 

Matt Warren covers this extensively in his "Building an IQueryable provider" series.

Greg Beech