将SQL查询转换为LinQ

时间:2012-08-01 09:05:24

标签: sql linq linq-to-sql

由于来自客户的奇怪请求,我已设法弄清楚如何实施它 使用SQL查询,但我无法将其翻译成LinQ。

 SELECT (SELECT count(*) FROM table1 where attribute1 like 'value1'),
        (SELECT count(*) FROM table2 where attribute2 like 'value2')

查询到LinQ的翻译是什么?

2 个答案:

答案 0 :(得分:1)

var count1 = (from i in table1 where SqlMethods.Like(i.attribute1, "value1") select i).Count();
var count2 = (from i in table2 where SqlMethods.Like(i.attribute2, "value2") select i).Count();

答案 1 :(得分:1)

您可以为Count()函数提供谓词

var result = new { 
            Count1 = table1.Count(r => r.attribute1.Contains("value1")), 
            Count2 = table2.Count(r => r.attribute2.Contains("value2")) 
        };