【发布时间】:2010-11-12 08:37:17
【问题描述】:
如何将此查询从 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;
【问题讨论】:
-
我认为您应该注意 Eamon 在他回答的第二个要点中的警告。
-
实际上,
.Contains()调用确实正确转换为 LinqToSql(也可能转换为 EF)-blog.wekeroad.com/2008/02/27/… -
阅读该链接后,还值得注意的是,使用数组而不是 List 将生成 IN 语句。
标签: sql linq linq-to-sql