Consider three IQueryables: tableA, tableB, and tableC. They all have a composite key of 3 values, a, b, and c. I will need to get a sum amount of a column in tableB, d, partitioned by a, b, and c. How do I join the three tables together to get an accurate partition to select a new object? My compiler always shows an error.
here is my basic query that I have tried:
from A in tableA
join B in tableB on new { A.a, A.b, A.c } equals new { B.a, B.b, B.c } into p1
group B by new { B.a, B.b, B.c } into p2
from q2 in p2
join C in tableC on new { A.a, A.b, A.c } equals new { C.a, C.b, C.c } into p3
from results in p3
select new NewObject
{
Column1 = p3.A.a,
Column2 = p3.A.b,
Column3 = p3.A.c,
Column4 = Sum(p3.C.d),
Column5 = p3.B.e
};
I get this long error about ambiguous invocation and I get red lines all over the place. I guess I'm not too sure how to do multiple joins onto a group by'd IQueryable. Is that even possible?
I think in this scenario, your query could benefit by simplifying the joins, writing a select with all of the required columns, and then utilizing the fluent API as follows. I believe this query will achieve your desired result.
var model = (from A in tableA
join B in tableB on new { A.a, A.b, A.c } equals new { B.a, B.b, B.c }
join C in tableC on new { A.a, A.b, A.c } equals new { C.a, C.b, C.c }
select new {
Aa = A.a,
Ab = A.b,
Ac = A.c,
Ba = B.a,
Bb = B.b,
Bc = B.c,
Be = B.e,
Ca = C.a,
Cb = C.b,
Cc = C.c,
Cd = C.d
})
.GroupBy(x => new {x.Ba, x.Bb, x.Bc})
.Select(grp => new {
Column1 = grp.First().Aa,
Column2 = grp.First().Ab,
Column3 = grp.First().Ac,
Column4 = grp.Sum(y => y.Cd),
Column5 = grp.First().Be
}).ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.