I am building a where-condition-string dynamically from user inputs to query on an ObjectSet.
So I've got something like:
// Filter name
whereConditions.Add("it.Name=@Name");
parameters.Add(new ObjectParameter("Name", model.Name));
// Filter Bonuspoints
whereConditions.Add("it.CustomerBonusPoint.BonusPoints=@BonusPoints");
parameters.Add(new ObjectParameter("BonusPoints", model.BonusPoints));
// Query
db.persons.Where(whereConditions, parameters.ToArray());
Which works perfectly even for related objects as you can see above.
But somehow I am not able to use aggregates. This doesn't work:
whereConditions.Add("it.CustomerSaleFigures.Sum(x=>x.Turnover)>@Turnover");
parameters.Add(new ObjectParameter("Turnover", model.Turnover));
Am I missing something? Any help appreciated!
The string conditions used by ObjectQuery
are Entity SQL, not C# Linq.
whereConditions.Add(@"
Sum(
SELECT Turnover
FROM NAVIGATE(it.CustomerSaleFigures)
) > @Turnover");
parameters.Add(new ObjectParameter("Turnover", model.Turnover));
(I'm not sure if NAVIGATE()
is required)
Alternatively, you could use Linq:
IQueryable<Person> query = db.persons;
query = query.Where(c => c.Name == model.Name);
query = query.Where(c => c.CustomerBonusPoint.BonusPoints == model.BonusPoints);
query = query.Where(c => c.CustomerSaleFigures.Sum(sf => sf.Turnover) > model.Turnover);
return query.ToList();
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.