实体框架-有条件地将列添加到分组依据 [英] Entity Framework - Conditionally adding columns into group by
本文介绍了实体框架-有条件地将列添加到分组依据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想有条件地将列添加到GroupBy子句中,但是我不确定该怎么做.
I want to conditionally add columns into GroupBy clause but I am not sure how it can be done.
我有5列,我想根据用户的输入按语句添加到组中.为此,我具有以下属性:
I have 5 columns which I want to add in group by statement depending upon user's input. To do this I have following properties:
ProductId
ColorId
PieceId
SizeId
WeightId
如果任何属性的值大于0,则需要在group by子句中使用此列.
If any property has value greater than 0 then this column needs to be used in group by clause.
因此,如果ProductId,ColorId和PieceId的值大于0,则在分组依据中应存在以下值:
So if ProductId, ColorId and PieceId has value greater than 0 than following should be there in group by:
list.GroupBy(p => new { p.Product.Id, p.ColorId, p.PieceId });
推荐答案
尝试类似方法;
var groupedList = list.GroupBy(p => new
{
ProductId = p.Product.Id > 5 ? p.Product.Id : (int?)null,
ColorId = p.ColorId > 5 ? p.ColorId : (int?)null,
PieceId = p.PieceId > 5 ? p.PieceId : (int?)null
})
.Select(x =>
new
{
x.Key.ProductId,
x.Key.ColorId,
x.Key.PieceId
}).ToList();
这篇关于实体框架-有条件地将列添加到分组依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文