简体   繁体   中英

using PredicateBuilder inside a select

I'm creating an Ad-hoc report feature for my application. I used the PredicateBuilder for the "Where" part but now when I'm trying to use it also inside the "Select" part I can't succeed.

Sample of the code:

   IQueryable<User> usersQuery = db.Users.AsQueryable();

    var where = PredicateBuilder.True<User>();
    //sample for the users query
    where = where.And(p => p.Enabled);


    var selectOrders = PredicateBuilder.True<UserOrder>();
    //sample for a query inside user orders
    selectOrders = selectOrders.And(p => p.Amount > 10);

    usersQuery = usersQuery.Where(where); //work
    var query = (from a in usersQuery
         select new
                {
                FirstName = a.FirstName,
                TotalOrders = a.UserOrders.Where(selectOrders).Count() //could not compile                                                                                               
                }).AsQueryable();

I don't think you can do

.Select("new (TotalOrders = UserOrders.Where(BetAmount > @0).Count()")

In DynamicLinq but I can't use it cause I call some sql user defined functions inside the 'select' and DynamicLinq doesn't support it.

I think problem here lies in fact, that Linq to SQL doesnt support subqueries, which would be required by your query. Thats why navigational properties UserOrders in your case doesn't implement IQueryable and queries can't be run on it.

I might be wrong. But this proves me right : http://blog.robustsoftware.co.uk/2009/01/why-linq-to-sql-is-not-proper-orm.html

You should use better ORM like Entity Framework.

What about a workaround like this? You have change the join condition according to your schema

var where = PredicateBuilder.True<User>();
//sample for the users query
where = where.And(p => p.Enabled);

var selectOrders = PredicateBuilder.True<UserOrder>();
//sample for a query inside user orders
selectOrders = selectOrders.And(p => p.Amount > 10);

var usersQuery = from u in db.Users.Where(where)
                 join o in db.UserOrders.Where(selectOrders) 
                        on u.UserId equals o.UserId
                 group o by u into groupedOrders
                 select new
                 {
                    FirstName = groupedOrders.Key.FirstName,
                    TotalOrders = groupedOrders.Count()                                                                                              
                 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM