如何使用linq模拟Derived Table
考虑此代码:
SELECT * FROM (SELECT * FROM Mytbl) AS tmp WHERE tmp.ID=1
我如何用Linq写这个?
感谢
编辑1:
如何将其转换为linq?:
select
convert( decimal(10,1), ROUND(A.c2*100,8)),
convert( decimal(10,1), ROUND(A.c3*100,8))
from
(
SELECT
(
SELECT CONVERT(INT, COUNT(ID))
FROM tbl
WHERE (col06 >= @param_Min and col06 <= @param_Max )
)
/
(
SELECT CONVERT(INT, COUNT(ID))
FROM tbl
WHERE (col06 >= 10 )
) as c2
(
SELECT CONVERT(INT, COUNT(ID))
FROM tbl
WHERE (col06 >= @param_Min and col06 <= @param_Max )
AND (col03 = 1)
)
/
(
SELECT CONVERT(INT, COUNT(ID))
FROM tbl
WHERE (col06 >= 10 ) AND (col03 = 1)
) as c3
) AS A
答案 0 :(得分:2)
**SELECT * FROM (SELECT * FROM Mytbl) AS tmp WHERE tmp.ID=1**
Linq版
var result = db.MyTbl.Where( x => x.ID == 1);
2
var query =
let c1= from b in db.MyTbl
where b.col06 >= @param_Min//some condition
select b.ID
let c2= from b in db.MyTbl
where b.col06 >= 10//some condition
select b.ID
let c3=from b in db.MyTbl
where b.col06 >= @param_Min//some condition
select b.ID
select new
{
c2.ID,//perform Round and other operation on this
c3.ID//perform Round and other operation on this
};
答案 1 :(得分:0)
WITH(NOLOCK)
无法在LINQ-to-Entities中实现,因此您应该将其包装在存储过程中并调用它。
通常这是计算而不是数据集检索,所以它不太适合Linq到实体。使用EF计算时,应在应用程序中完成,因此应分别调用四个子查询并在应用程序中计算结果。作为替代方案,您可以使用存储过程。