Dan
Dan

Reputation: 35

Linq multiple left outer joins with grouping

I have a player class object which contains an image as one of it's properties. I am trying to do a left join to an equipments class and a left join to a social class. I want to retrieve all players and if the player has equipment or social properties, set accordingly, if the equipment or social is null, leave as null but still show the player.

I am currently getting a "The image data type cannot be selected as DISTINCT because it is not comparable." error. The query I am trying to do:

var player = (from p in db.Players
              join e in db.Equipment on p.PlayerId equals e.PlayerId
              into playerequipment
              from d in playerequipment.DefaultIfEmpty()
              join so in db.Social on p.PlayerId equals so.PlayerId
              into playersocial
              from de in playersocial.DefaultIfEmpty()
              where p.PlayerId == PlayerId
              group new { p, d, de } by new
              {
                  Player = p
              } into x
              select new 
              { 
                  Player = x
              });

Thanks

Upvotes: 0

Views: 69

Answers (1)

Florian Schmidinger
Florian Schmidinger

Reputation: 4692

Try using Navigation Properties:

var players = db.Players.Where(player=> player.Equipment != null && player.Social != null).ToList();

No clue if your Navigation Properties are named this way though... Check your model.

If you want to retrieve them regardless of the presence of Social or Equipment:

var players = db.Players.Include("Social").Include("Equipment").ToList();

Upvotes: 1

Related Questions