如何在(linq 和 lambda)中将 select 与 max,min 函数一起使用?
How, I can use select with max,min function in (linq and lambda)?
我需要使用 linq 和 lambda 搜索最大值和最小值。我有sqlselect,例如:
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 查询。唯一的 "important" 是 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)
}
我需要使用 linq 和 lambda 搜索最大值和最小值。我有sqlselect,例如:
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 查询。唯一的 "important" 是 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)
}