我有一个名为Outweigh的表,它有大约50个字段。我需要创建一个类似于这个SQL查询的Linq查询。将SQL语句转换为Linq?
SELECT DISTINCT Product,
Medication,
SUM(NettWeight) as NettWt,
COUNT(DISTINCT CustCode) as Customer,
COUNT(DISTINCT DktNo)as DktNo
FROM outweigh
GROUP BY Product, Medication
ORDER BY Product, Medication
结果如下:
我已经写了一些代码,如下这是不是很有效。
lstOutWeigh = dbContext.Outweighs
.Where(o => o.DateOut >= StartDate && o.DateOut <= EndDate)
.ToList();
var k = from t in lstOutWeigh select new {t.Product,t.Medication,t.NettWeight};
var l = from t in k
group t by new { t.Product, t.Medication } into g
select new someclass
{
prod =g.Key.Product,
med = g.Key.Medication,
tonnes = (double) g.Sum(x => x.NettWeight),
count = g.Count()
};
有什么建议吗?
http://sqltolinq.com/ – SQLMason
什么是lstOutWeigh?它是如何填充的?您可能会无意中将数据库中的所有记录撤回,并将linq转换为对象,而不是将linq转换为实体。 – gareththegeek
“效率不高”似乎是你问题的关键部分,但是这并不能提供足够的细节让我们真正去帮助。如果您的Linq声明有效,但您要求提供性能帮助,请更清楚。 –