![](/img/trans.png)
[英]How can I use Expression.Lambda for SELECT LINQ operation
[英]How, I can use select with max,min function in (linq and lambda)?
我需要使用linq和lambda搜索最大值和最小值。 我有sql选择,例如:
SELECT
[ProdID]
,min([APY]) as minAPY
,max([APY]) as minAPY
FROM [dbase].[dbo].[Dept]
group by ProdID
order by ProdID
谢谢你的帮助!
查询应该是这样的:
var res = from x in db.Debt
group x by x.ProdID into y
orderby y.Key
select new
{
ProdID = y.Key,
minAPY = y.Min(z => z.APY),
maxAPY = y.Max(z => z.APY)
};
如您所见,它很好地反映了TSQL查询。 唯一“重要”的事情是在group
之后into y
(继续查询所必需的)
尝试这个:
var result = from table in ContextDB.Dept
group table by table.ProdID into tableGroup
order by table.ProdId
select new
{
ProdID = tableGroup.Key,
minAPY = tableGroup.Min(item => item.APY),
maxAPY = tableGroup.Max(item => item.APY)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.