Search code examples
c#linqjoiniqueryableenumerable

Use an additional condition/parameter for Enumerable.GroupJoin


I looked at the documentation for the GroupJoin method at MSDN but I don't see how one would create the join on more than one property. Below is an example on how my current code looks like.

  var test = _MasterData.LinqCustomerGroup.GroupJoin(_MasterData.LinqDisposallistDisposalArticles,
                                                           c => c.Guid,
                                                           d => d.CustomerGroup,
                                                           (c, ds) => new { c, ds = ds })                                                    
                                                .SelectMany(z => z.ds.DefaultIfEmpty(), (c, d) => new { CustomerGroupID = c.c.CustomerGroupID, Description = c.c.Description, Disposallist = d.Disposallist }));

I would like to have the join be performed on an additional parameter. Inside d is an attribute called Article. I want the function to only join elements that have Article == "value".


Solution

  • It sounds like you don't want to "join on an additional condition", but to filter out all elements in LinqDisposallistDisposalArticles that have an Article different from "value".

    If that's correct, simply use Where on that sequence before joining:

    var test = _MasterData.LinqCustomerGroup.GroupJoin(
                    // filter inner sequence
                    _MasterData.LinqDisposallistDisposalArticles.Where(d => d.Article == "value"),
                    c => c.Guid,
                    d => d.CustomerGroup,
                    (c, ds) => new { c, ds = ds })                                                    
               .SelectMany(....);