LINQ: How to do JOIN using the linq extension meth

2019-02-05 07:21发布

问题:

In the Join below, I'd like to use multiple fields to do the join rather than just one field.

var join = group.Join(procSums, g => g.DeptID, ps => ps.key.deptID, (g, ps)...

All examples Ive found use the query style to do this and I can't translate it.

Thanks!

回答1:

You just have to Join based on new anonymous objects:

// ClasID is a placeholder, it could be any property you'd like
var join = group.Join(procSums,
                      g => new { g.DeptID, g.UnitLoc, g.Proc },
                      ps => new 
                            { 
                              DeptID = ps.key.deptID, 
                              UnitLoc = ps.key.unitLoc,
                              Proc = ps.key.procName 
                            },
                      (g, ps) => new { g, ps });


回答2:

You need to pass lambda expressions that create anonymous types with the fields.
For example:

group.Join(procSums, g => new { g.DeptID, g.OtherField }, ps => new { ps.key.deptID, ps.key.OtherField }, ...)

The anonymous types must match exactly.



标签: linq join field