我的代码中有这个:
var pRating = ctx.PrProjectRating
.Where(x => x.PrIsDeleted == false)
.Select(k => k)
.GroupBy(g =>
new {g.PrPIdG},
(key, group) => new
{
sumR = group.Sum(k => k.PrValue),
pidG = key.PrPIdG
});
var pLike = ctx.PlProjectLike
.Where(x => x.PlValue == "Like")
.Select(c => c)
.GroupBy(g =>
new {g.PlPIdG},
(key, group) => new
{
sumR = group.Count(),
pidG = key.PlPIdG
})
.OrderByDescending(g => g.sumR);
var pConnect = ctx.PcProjectConnect
.Where(x => x.PcStatus == "Connected")
.Select(c => c)
.GroupBy(g =>
new {g.PcPIdG},
(key, group) => new
{
sumR = group.Count(),
pidG = key.PcPIdG
})
.OrderByDescending(g => g.sumR);
如何组合这些集合并将sumR值相加?
编辑
pRating =
pidG sumR
123 11
124 7
125 5
pLike =
pidG sumR
123 3
125 2
pConnect =
pidG sumR
125 5
结果应该是:
pResult =
pidG sumR
123 15
125 12
124 7
我需要将pidG分组在一起,并使用sumR-进行汇总
我想得到pidG值的列表,将它们分组,找到计数或总和,并按最高总和值对它们进行排序,这就是你在上面的集合和表格图中看到的。
然后我需要获取总和并对集合进行分组,以发现其按sumR 的最高值排序
编辑
我正试图做到这一点:
var query =
from i in ids
join ra in ratings on i equals ra.Id into rs
from ra in rs.DefaultIfEmpty()
join l in likes on i equals l.Id into ls
from l in ls.DefaultIfEmpty()
join co in connects on i equals co.Id into cs
from co in cs.DefaultIfEmpty()
select new
{
Id = i,
Total = ra?.Sum ?? 0 + l?.Count ?? 0 + co?.Count ?? 0,
Ratings = ra?.Sum ?? 0,
Likes = l?.Count ?? 0,
Connects = co?.Count ?? 0,
};
query = query.OrderByDescending(x => x.Total);
但并没有把我需要的总数加起来。
您应该能够将所有查询作为子查询单独进行,然后进行完全联接以在客户端上组合结果。
var ratings =
from r in ctx.PrProjectRating
where !r.PrIsDeleted
group r.PrValue by r.PrPIdG into g
select new
{
Id = g.Key,
Sum = g.Sum(),
};
var likes =
from l in ctx.PlProjectLike
where l.PlValue == "Like"
group 1 by l.PlPIdG into g
select new
{
Id = g.Key,
Count = g.Count(),
};
var connects =
from c in ctx.PcProjectConnect
where c.PcStatus == "Connected"
group 1 by c.PcPIdG into g
select new
{
Id = g.Key,
Count = g.Count(),
};
var ids = ratings.Select(r => r.Id)
.Union(likes.Select(l => l.Id))
.Union(connects.Select(c => c.Id))
.ToHashSet();
var query =
from i in ids
join r in ratings on i equals r.Id into rs
from r in rs.DefaultIfEmpty()
join l in likes on i equals l.Id into ls
from l in ls.DefaultIfEmpty()
join c in connects on i equals c.Id into cs
from c in cs.DefaultIfEmpty()
select new
{
Id = i,
Ratings = r?.Sum ?? 0,
Likes = l?.Count ?? 0,
Connects = c?.Count ?? 0,
};