首页 新闻 会员 周边 捐助

[急]Linq求助

0
悬赏园豆:5 [已解决问题] 解决于 2008-08-29 09:00
<P>小弟刚着手学习Linq,脑袋好象有些转不过来弯,大家帮忙写个语句吧,谢谢了,表结构如下:</P> <P>BigClass表<BR>b_id&nbsp;&nbsp; name<BR>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;软件工程<BR>2&nbsp;&nbsp;&nbsp;&nbsp; 软件测试<BR>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数据库开发</P> <P>News表<BR>id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; title&nbsp;&nbsp; content&nbsp;&nbsp; time&nbsp; b_id(外键)<BR>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; title1&nbsp;&nbsp; fasdfs&nbsp;&nbsp;&nbsp; null&nbsp;&nbsp; 2<BR>2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; title1&nbsp;&nbsp; fasdfs&nbsp;&nbsp;&nbsp; null&nbsp;&nbsp; 1<BR>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; title1&nbsp;&nbsp; fasdfs&nbsp;&nbsp;&nbsp; null&nbsp;&nbsp; 2<BR>4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; title1&nbsp;&nbsp; fasdfs&nbsp;&nbsp;&nbsp; null&nbsp;&nbsp; 3<BR>5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; title1&nbsp;&nbsp; fasdfs&nbsp;&nbsp;&nbsp; null&nbsp;&nbsp; 2<BR>6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; title1&nbsp;&nbsp; fasdfs&nbsp;&nbsp;&nbsp; null&nbsp;&nbsp; 3<BR>7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; title1&nbsp;&nbsp; fasdfs&nbsp;&nbsp;&nbsp; null&nbsp;&nbsp; 1</P> <P>查询出如下结果:<BR>b_id&nbsp;&nbsp; name&nbsp;&nbsp;&nbsp;&nbsp; number<BR>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;软件工程&nbsp;&nbsp;&nbsp;&nbsp; 2<BR>2&nbsp;&nbsp;&nbsp;&nbsp; 软件测试&nbsp;&nbsp;&nbsp;&nbsp; 3<BR>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数据库开发&nbsp;&nbsp;&nbsp;2</P> <P>SQL语句实现很简单:<BR>select b.b_id as id,b.name as name,count(n.b_id) as number <BR>from news as n,bigclass as b<BR>where n.b_id=b.b_id<BR>group by n.b_id,b.b_id,b.name<BR><BR>由于刚开始学Linq,希望大家帮帮忙写下实现这个功能的Linq,谢谢了!</P>
问题补充: 大虾们,好象不行啊,有异常.
Bēniaǒ的主页 Bēniaǒ | 小虾三级 | 园豆:692
提问于:2008-08-10 22:49
< >
分享
最佳答案
0
var query = from b in db.BigClasses join n in db.News on b.b_id == n.b_id group new {b,n} by new {b.b_id,b.name} into g select new {Id = g.Key.b_id,Name = g.Key.name,Count = g.Count()} 上面的是采用inner join 的连接方式 下面的是采用标准SQL的连接方式,也就是你写的那种 var query = from b in db.BigClasses from n in db.News on b.b_id == n.b_id group new {b,n} by new {b.b_id,b.name} into g select new {Id = g.Key.b_id,Name = g.Key.name,Count = g.Count()}
小树爸爸 | 菜鸟二级 |园豆:210 | 2008-08-11 10:31
其他回答(3)
0
mark,我也想知道
lexus | 园豆:0 (初学一级) | 2008-08-11 09:33
0
var v = from b in db.BigClasses select new { Id = b_id, Name = name, Number = db.News.Count(x => x.id == b_id), };
deerchao | 园豆:8367 (大侠五级) | 2008-08-11 09:58
0
修改一下楼上的。 var query = from b in db.BigClasses join n in db.News on b.b_id equals n.b_id group new {b,n} by new {b.b_id,b.name} into g select new {Id = g.Key.b_id,Name = g.Key.name,Count = g.Count()} 楼上的正解。
不若相忘于江湖 | 园豆:51 (初学一级) | 2008-08-11 11:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册