如何将此查询从SQL转换为Linq:
SELECT status As 'Status',
count(status) As 'Count'
FROM tbl_repair_order
WHERE contract = 'con' and
(status = 'Parts Arr' or
status = 'NA' or
status = 'New Call' or
status = 'Parts Ord' or
status = 'Parts Req' or
status = 'F Work')
GROUP BY status
更新
谢谢大家,这是我用过的代码。测试并返回与上面相同的内容:
List<string> statuses = new List<string> { "Parts Arr", "NA", "New Call", "Parts Ord", "Parts Req", "F Work"};
var result = (from x in db.tbl_repair_orders
where x.CONTRACT == strContract
&& statuses.Contains(x.STATUS)
group x.STATUS by x.STATUS into grouping
select new { Status = grouping.Key, Count = grouping.Count() });
return result;
答案 0 :(得分:4)
string[] statuses = new string[] { "Parts Arr", "NA", "New Call", "Parts Ord", "Parts Req", "F Work" };
var x = (from ro in db.tbl_repair_order
where ro.contract == "con"
&& statuses.Contains(ro.status)
group 0 by ro.status into grouping
select new { Status = grouping.Key, Count = grouping.Count() });
我不知道语法是否正确(尤其是最后两行)但它应该非常接近。
我在小组之间添加了0,并根据Eamon Nerbonne在评论中的更正。另外,感谢Ryan Versaw提供了解释List和用于生成IN子句的数组的链接。
答案 1 :(得分:2)
假设您正确地连接了表格,例如
var statusCounts =
from row in youDbNameHere.tbl_repair_order
where row.contract == "con"
&& (row.status == "Parts Arr"
|| row.status == "NA"
|| row.status == "New Call"
|| row.status == "Parts Ord"
|| row.status == "Parts Req"
|| row.status == "F Work")
group 0 by row.status into g
select new { Status = g.Key, StatusCount = g.Count() };
......我看到安迪打败了我; - )
注意:
答案 2 :(得分:-1)
至于将SQL语句转换为等效的Linq to SQL语句,您应该查看Linqer工具。我认为这个应用程序不适合用于重写整个应用程序,但它通常是学习Linq to SQL的非常有用的工具。