我花了最近两个小时研究这个。 LINQ btw是新手。 如何将这样的SQL转换为LINQ查询?
SELECT r.id, name, designation, COUNT(a.id)
FROM region r
LEFT JOIN area a on a.region_id = r.id
GROUP BY r.id, name, designation
我当然可以选择将其置于View
(我可以弄清楚)中,但我想将此添加到我的知识中。谢谢!
P.S。 VB或C#都可以,但我更喜欢前者。
答案 0 :(得分:1)
我认为会是这样的:
var result =
from r in regions
join a in areas on r.id equals a.region_id into subvalues
select new { id = r.id, name = r.name, designation = r.designation, count = subvalues.Count() };
答案 1 :(得分:0)
假设您要对数据进行分组,请尝试查看类似于以下内容的查询:
account invoicedate ageamount
1-129285408641 2016-02-08 92
1-129285650772 2016-02-08 666
1-129285408641 2016-07-08 717
1-129285650772 2016-07-08 885
1-129285650772 2015-09-08 766
1-129285408641 2016-05-08 1479
1-129285650772 2016-05-08 637
1-129285650772 2015-10-08 682
1-129285408641 2016-03-08 668
1-129285650772 2016-01-08 637
1-129285650772 2015-08-08 439
1-129285650772 2015-12-08 641
1-129285650772 2015-07-08 109
1-129285408641 2016-06-08 183
1-129285650772 2016-06-08 914
1-129285650772 2016-08-08 415
1-129285408641 2016-08-08 1198
1-129285650772 2016-10-08 579
1-129285408641 2016-11-08 250
1-129285650772 2016-11-08 1148
1-129285650772 2015-11-08 694
1-129285408641 2015-09-08 1363
1-129285650772 2016-03-08 748
1-129285408641 2016-01-08 1347
1-129285408641 2015-11-08 442
1-129285408641 2015-08-08 409
1-129285408641 2015-12-08 918
1-129285408641 2015-07-08 109
1-129285408641 2016-04-08 421
1-129285650772 2016-04-08 637
1-129285650772 2016-09-08 1000
1-129285408641 2016-09-08 119
1-129285408641 2016-10-08 1228
答案 2 :(得分:0)
这样的事也可以正常工作..
int count = 0;
(from r in region
join a in area on r.<join_val> equals a.<join_val> into leftJ
from lj in leftJ.DefaultIfEmpty()
select lj
).GroupBy( r => r.id).All(item =>
{
count += item.Count();
return true;
});
count应具有您正在寻找的值,