如何将这个内连接T-SQL转换为LINQ到实体?

时间:2016-02-11 06:38:22

标签: c# sql-server linq tsql

我有一段存储过程:

...
SELECT B.BinID, AverageCost, SUM(Qty) AS Qty
FROM #CurrentReturn R INNER JOIN Bins B ON R.BinCode = B.BinCode AND B.StoreroomID = @StoreroomID
...

@StorerroomID是SP参数之一。

现在我正在尝试将其转换为LINQ to Entities,

var AverageCostList = from r in CurrentReturn
                      join b in BinQuery on new {r.BinCode, b.StoreroomID} equals new {b.BinCode, storeroomID}

它不起作用,如L.H.S.上的类型。 equals的{​​}不能包含b。

中的字段

那么有没有办法将这样的内连接SQL转换为LINQ?

1 个答案:

答案 0 :(得分:1)

我会将B.StoreroomID = @StoreroomID比较放入where where子句

from r in CurrentReturn
join b in BinQuery 
on r.BinCode equals b.BinCode
where b.StoreroomID == storeroomID