如何将SQL Join查询转换为LINQ? [英] How to convert SQL Join query into LINQ?
问题描述
我需要将此代码转换为linq查询。
下面的查询让我在MS SQL中更正了总数。
SELECT SUM(t1.itemPrice + t2.itemPrice)为TOTAL_AMOUNT_DUE FROM table1 t1,table2 t2 WHERE r.userID = u.userID
我试图让这个相同的代码在linq查询中工作,所以我可以在我的项目MVC4中使用它。
我的尝试失败主要是b / c我还不熟悉Linq。这是:
--Linq--
var query =(从db.table1中的t1加入db.table2中的table2 t1.userID等于t2.userID
选择新{SUM(t2.itemPrice + t1.itemPrice)});
显然以上不工作。有人可以帮忙吗?
谢谢!
Ceci
我认为这应该有效
检查是否存在语法错误
(来自 p table1
join q 在 table2中等于q.userID
选择 p.itemPrice + q.itemPrice).ToList() .SUM()
I need to convert this code into linq query.
The query below gives me to correct total in MS SQL.
SELECT SUM(t1.itemPrice + t2.itemPrice) as TOTAL_AMOUNT_DUE FROM table1 t1, table2 t2 WHERE r.userID = u.userID
I am trying to get this same code to work in linq query so I can use it in my project MVC4.
My attempt is failing mainly b/c I am not very familiar with Linq just yet. here it is:
--Linq--
var query = (from t1 in db.table1 join table2 in db.table2 on t1.userID equals t2.userID
select new { SUM(t2.itemPrice + t1.itemPrice) });
Obviously the above don't work. Can anyone help?
Thank you!
Ceci
I think this should work
check for syntax errors if any
(from p in table1 join q in table2 on p.userID equals q.userID select p.itemPrice + q.itemPrice).ToList().Sum()
这篇关于如何将SQL Join查询转换为LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!