Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 802
Shay Avatar asked Sep 04 '25 17:09

Shay


2 Answers

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.

like image 70
Euphoric Avatar answered Sep 07 '25 09:09

Euphoric


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()                                                                                              
                 };
like image 33
Nasmi Sabeer Avatar answered Sep 07 '25 09:09

Nasmi Sabeer