LINQ TO EF
表 MagazineID title
1 1111
2 2222
3 3333
4 4444
5 5555
6 6666
7 7777
8 8888
表 Article
ID MagazineID MagazineToID
1 1 3
2 1 3
3 1 4
4 1 4
5 2 6
6 2 1
7 3 8
需要统计 Article表 MagazineID 和MagazineToID出现最多次数的数据
需要结果:
1出现了5次 所以排第一
3出现3次 排第二
表 Magazine
ID title
1 1111
3 3333
2 2222
4 4444
6 6666
8 8888
5 5555
7 7777
请教下该怎么写 linq --------------------编程问答-------------------- 木有人。。。。 --------------------编程问答-------------------- var IDs = ( from a in Article select new{ID=a.MagazineID } ).Concat( from a in Article select new{ID=a.MagazineToID } );
var IDCount = from p in IDs group p by p.ID into g select new { Id=g.Key, Nums = g.Count()};
var result = from m in Magazine
join p in IDCount on m.ID equals p.ID
order by p.Nums
select new {m.ID,m.title};
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/ --------------------编程问答-------------------- var query=(from a in Magazine
jion b in Article on a.id equals b.MagazineID
jion c in Article on a.id equals c.MagazineToID
group {a,b,c} by a.id into g
select new
{g.key.id,
g.key.title
}).orderby(a=>g.key.count()).distinct();
不知道这个思路能行的通不 具体语句怎么写我也没试验过 --------------------编程问答-------------------- --------------------编程问答-------------------- 在数据库中处理好,然后一次性返回想要的结果呀
create table Magazine(ID int, title int)
insert into Magazine
select 1,1111
union all select 2,2222
union all select 3,3333
union all select 4,4444
union all select 5,5555
union all select 6,6666
union all select 7,7777
union all select 8,8888
create table Article(ID int, MagazineID int, MagazineToID int)
insert into Article
select 1,1,3
union all select 2,1,3
union all select 3,1,4
union all select 4,1,4
union all select 5,2,6
union all select 6,2,1
union all select 7,3,8
select a.*,b.num
from Magazine a
left join
(
select MagazineID,COUNT(*) as num
from (
select MagazineID from Article
union all select MagazineToID from Article
)t group by MagazineID
)b
on a.ID=b.MagazineID
order by b.num desc
drop table Magazine,Article
/*
ID title num
1 1111 5
3 3333 3
4 4444 2
2 2222 2
6 6666 1
8 8888 1
7 7777 NULL
5 5555 NULL
*/
--------------------编程问答--------------------
好的我试试 --------------------编程问答--------------------
恩 是的 倒序 --------------------编程问答--------------------
var query = from obj in
((from item in ArticleList
group item by item.MagazineID into g
select new
{
ID = g.Max(a => a.MagazineID).ToString(),
Count = g.Count().ToString()
}).Concat
(
from item in ArticleList
group item by item.MagazineToID into g
select new
{
ID = g.Max(a => a.MagazineToID).ToString(),
Count = g.Count().ToString()
}
))
group obj by obj.ID into gGroup
select new
{
ID = gGroup.Key,
Count = gGroup.Sum(a => Convert.ToInt32(a.Count))
};
query = query.OrderByDescending(a=>a.Count).ToList();
补充:.NET技术 , LINQ