gpt4 book ai didi

c# - 带有聚合和分组依据的 LINQ 查询

转载 作者:行者123 更新时间:2023-11-30 22:24:09 35 4
gpt4 key购买 nike

我有以下 SQL 查询...

    select  seaMake AS Make,
seaModel AS Model,
COUNT(*) AS [Count],
MIN(seaPrice) AS [From],
MIN(seaCapId) AS [CapId]
from tblSearch
where seaPrice >= 2000
and seaPrice <= 7000
group by seaMake, seaModel
order by seaMake, seaModel

我试图将其编写为 LINQ to Entities Query,但我遇到了问题。这是我目前所拥有的,但我无法从 var S 访问品牌和型号值

var tester = from s in db.tblSearches
where s.seaPrice >= 2000
&& s.seaPrice <= 7000
orderby s.seaMake
group s by s.seaMake into g
select new
{
make = g.seaMake,
model = s.seaModel,
count = g.Max(x => x.seaMake),
PriceFrom = g.Min(s.seaPrice)
};

我哪里错了?

最佳答案

这应该是 SQL 的直接翻译:

from s in db.tblSearches
where
s.seaPrice >= 2000 &&
s.seaPrice <= 7000
group s by new {s.seaMake, s.seaModel} into g
orderby g.Key
select new
{
Make = g.Key.seaMake,
Model = g.Key.seaModel,
Count = g.Count(),
From = g.Min(x => x.seaPrice),
CapId = g.Min(x => x.seaCapId)
}

关于c# - 带有聚合和分组依据的 LINQ 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12941794/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com