0

I cannot seem to figure out how to do a full outer join in Linq using lambda expressions. Here is a stripped down sample of what I'm trying to do.

void Main()
{
var myPets = new List<petInfo>();
var yourpets = new List<petInfo>();

myPets.Add(new petInfo("me", "dog","Rex"));
myPets.Add(new petInfo("me", "fish","Goldy"));
myPets.Add(new petInfo("me", "iguana","Spikey"));

yourpets.Add(new petInfo("you", "dog","Spot"));
yourpets.Add(new petInfo("you", "cat","Tigger"));
yourpets.Add(new petInfo("you", "bird","Polly"));

var ourPets = myPets.GroupJoin(yourpets, mine => mine.PetType, yours => yours.PetType, (mine, yours) => new { WHAT GOES HERE???});
}

where petInfo is a boring class:

public class petInfo
{
public string Owner;
public string PetType;
public string PetName;

public petInfo(string owner, string petType, string petName)
{
    Owner = owner;
    PetType = petType;
    PetName = petName;
    }
}

I'm expecting ourPets to have the following records after the outer join:

PetType MyOwnerName MyPetName YourOwnerName YourPetName
bird                          you           Polly
cat                           you           Tigger
dog     me          Rex       you           Spot
fish    me          Goldy
iguana  me          Spikey

I've figured out almost everything else with linq and lambda, but for some reason joins are causing me all sorts of grief. I usually wind up just looping over the two sets and creating a third manually, but I've decided I want to solve this once and for all. All of the examples I found here seem to be a bit off or have requirements specific to this problem. I haven't found a good basic lambda outer join yet.

edit I have looked at the referenced solution and would like to implement this without using an extension method if possible. If it isn't possible, then I'll do so, but I love linq and lambda's because the code is so clean and it just feels like this should be able to be accomplished in a much simpler manner.

CompuChip
  • 63
  • 6
  • https://gist.github.com/hoganlong/fe39ecbeb2a72c471821e83d5c845649 – Hogan Apr 26 '16 at 14:55
  • @Hogan - thanks for the link, but it is returning separeate results on Dog for "me" and "you" where I am trying to have one row where both sets have the same pet. – CompuChip Apr 26 '16 at 16:49
  • @usr / fubo - I was looking at it as a SQL outer join. I'll also edit the equest as I'd like to do so without extension methods if possible form the linked solution. – CompuChip Apr 26 '16 at 16:51
  • in that case you have to use one of the methods in the linked examples – Hogan Apr 26 '16 at 21:00

0 Answers0