0

I am trying to do a group join(GroupJoin) on a couple of fields including a string 'like'.

Let's say Human - {Name, Size, FullAddress} and Item - {OwnerName, City, Size}

let's say I go for a normal join

var result = from h in Human
                join it in Item on 
                    h.OwnerName equals it.OwnerName
                where h.Size == it.Size ||
                      h.Address.startsWith(it.City)   
                select new {human = h, Item = it};

I guess I can pretend that it works and gives all items that belong to a person with the same name and fit them, or are in the same city.

But trying to go for the group join kinda stops me in my tracks:

var result = from h in Human
                join it in Item on 
                    h.OwnerName equals it.OwnerName into g
                where h.Size == it.Size ||
                      h.Address.startsWith(it.City)   
                select new {human = h, Items = g};

ASFAIK it(item) is no longer defined for the 'where' context. If I did not have the 'LIKE%' part(startsWith) I probably would have been able to use the anonymous class helped join(as in this answer/question)

I am trying to figure some compact solution for this, without going back to the not grouped join and grouping them manually.

urmat abdykerimov
  • 427
  • 1
  • 7
  • 17
Wer Kolev
  • 1
  • 2
  • You do not need 'g'. Just use Items = it – jdweng Aug 21 '18 at 14:27
  • @jdweng thanks for the answer. I must admit that I have some issues understanding your answer, are you saying that there is no way to make the groups so I should use the first query from my question? – Wer Kolev Aug 22 '18 at 08:56
  • Into doesn't do a Group (see msnd : https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/into). I usually group by adding parenthesis around the from : (from ......select ......).GroupBy(x => ........) – jdweng Aug 22 '18 at 09:17
  • Oh, I am sorry, my bad. I was talking about [GroupJoin](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/join-clause#group-join), not GroupBy. – Wer Kolev Aug 22 '18 at 13:13
  • The only thing a group Join does is when you do a left outer join and want to find the items in B when A is null. – jdweng Aug 22 '18 at 16:43
  • 1
    You could put the condition on the `g` instead of on the overall match, since it is `g` you are trying to constrain, e.g. `Items = g.Where(it => it.Size == h.Size && h.Address.StartsWith(it.City))`. – NetMage Aug 23 '18 at 22:30
  • I kinda moved away from this code, so I have some issues validating this but @NetMage, your answer seems to be exactly what I was looking for. Thanks a lot. – Wer Kolev Aug 24 '18 at 14:14
  • @jdweng You're wrong about `into`. It *does* group because the compiler converts `join - into` into `GroupJoin`. See also [this](https://stackoverflow.com/a/15599143/861716). – Gert Arnold Aug 13 '21 at 15:19
  • @GertArnold : You are referencing your own posting. My link is a Microsoft Document. Yes Microsoft does make errors. But to say I'm wrong? – jdweng Aug 14 '21 at 15:29
  • @jdweng "Into doesn't do a Group" Yeah, but here it's used with `join`. That means `GroupJoin`. If not wrong then *at best* is highly confusing to say that grouping doesn't occur and then recommend `GroupBy`. – Gert Arnold Aug 14 '21 at 20:21

0 Answers0