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
UserOrdersin your case doesn’t implementIQueryableand 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.