Converting a lambda into an IPredicate

Posts   
 
    
Jez
User
Posts: 198
Joined: 01-May-2006
# Posted on: 02-Oct-2008 11:34:39   

Hi Frans,

Is there a method in LinqSupportClasses for converting a lambda to a RelationPredicateBucket? I had a quick look but nothing immediately jumped out at me.

I was hoping to add some extension methods to UnitOfWork2 to do this sort of thing:

var work = new UnitOfWork2();
work.AddDeleteEntitiesDirectlyCall<Customer>(x => x.Country == "UK");

rather than:

var work = new UnitOfWork2();
work.AddDeleteEntitiesDirectlyCall(typeof(Customer), 
    new RelationPredicateBucket(CustomerFields.Country == "UK")
);

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39612
Joined: 17-Aug-2003
# Posted on: 02-Oct-2008 16:15:11   

No that's not possible. The lambda is compiled to a delegate so it can't be converted. If you pass an Expression<> you have to parse the expression tree through teh whole linq provider but that's not really going to work I think. You could try with the QueryBuilder.HandleBinaryExpression but I think it's a dead end. The linq provider isn't setup for this: a lambda always have to have context within a larger queryable based query .

Frans Bouma | Lead developer LLBLGen Pro
Brandt
User
Posts: 142
Joined: 04-Apr-2007
# Posted on: 15-Nov-2008 19:09:57   

Otis wrote:

No that's not possible. The lambda is compiled to a delegate so it can't be converted. If you pass an Expression<> you have to parse the expression tree through teh whole linq provider but that's not really going to work I think. You could try with the QueryBuilder.HandleBinaryExpression but I think it's a dead end. The linq provider isn't setup for this: a lambda always have to have context within a larger queryable based query .

what about something like this that is done with Entity Framework.

db.OrderItems.DeleteAllOnSubmit( from si in db.OrderItems where !productsInBasket.Contains(si.ProductID) && si.OrderID == order.ID select si );

Just throwing it out there.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39612
Joined: 17-Aug-2003
# Posted on: 15-Nov-2008 20:47:49   

Though doesn't that query fetch the set of entities matching the query you pass in first, and then deleting them one by one?

If not, it could be a way, where the query is used to produce a fieldcompareset predicate, though, how such a query would be executed is then another struggle, so I'll leave it as-is for now.

Frans Bouma | Lead developer LLBLGen Pro