I need to convert Linq lambda Expression to IPredicate

Posts   
 
    
abdo
User
Posts: 10
Joined: 23-Feb-2009
# Posted on: 23-Feb-2009 18:24:27   

Dear LLBL Pro Team,

I'm facing a problem that I have a lambda expression and I need t pass it to GetDbCount to get the number of items in Database based on that expression, so how can I do that.

Thanks in advance. Abdo

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 23-Feb-2009 20:31:31   

You can't, but I think it's not that hard to formulate the predicate using normal llblgen pro constructs.

However, you could also use the 'Count()' operator in a linq query using Ling to LLBLGen Pro (our linq provider) to get the dbcount, and then you can use your lambda.

Frans Bouma | Lead developer LLBLGen Pro
abdo
User
Posts: 10
Joined: 23-Feb-2009
# Posted on: 23-Feb-2009 21:23:59   

First of all thanks a lot, second about the conversion from lambda expression to IPredicate if it's possible can you give an example or a key to start with grasp the idea of how i can do that, and also if i need to use that lambda expression to delete rows from Database how can i do that .

Thanks in advance.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 23-Feb-2009 21:47:25   

abdo wrote:

First of all thanks a lot, second about the conversion from lambda expression to IPredicate if it's possible can you give an example or a key to start with grasp the idea of how i can do that, and also if i need to use that lambda expression to delete rows from Database how can i do that .

Thanks in advance.

It would be easier I think if you could give the lambda you'd like to pass. Like, I want the count of the # of customers from germany: var count = (from c in metaData.Customer where c.Country=="Germany" select c).Count();

Frans Bouma | Lead developer LLBLGen Pro
abdo
User
Posts: 10
Joined: 23-Feb-2009
# Posted on: 23-Feb-2009 21:52:47   

Dear Otis,

I got the point about the count, but for example if i have a function like the following

IQueryable<Entity> FindALl( Expression<Func<Entity, bool>> exp).

and i need to pass that expression to some sort of LLBL functions that takes IPredicate can i do that.

and another something if I need to delete some rows from Database based on that expression as for example

EntityCollection coll = new EntityCollection();

coll.DeleteMulti(takesIPredicate) can i give it the lambda expression.

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 24-Feb-2009 10:59:57   

That's not possible.

How would you specify the type of the entity inside FindAll? A Expression<Func<T, bool>> is really a tree which has to be interpreted, but it has no value unless it's used inside a query, which means it's inside a Linq query (which in itself is an expression tree).

For example, your FindAll is really redundant, because it's the same as IQueryable<T>.Where, so why not do: var q = metaData.Entity.Where(exp);

?

So rule of thumb: either use full linq queries or use our own predicate system.

Frans Bouma | Lead developer LLBLGen Pro