简体   繁体   中英

C# Linq Join not equal

I am trying to create a linq join for a game that will select all the positions a player does not play in using the following query:

var m_player_positions = from pl in tfs.Players
                         join pos in tfs.Positions
                             on new { X = true } equals new { X = (pl.MainPositionID != pos.PositionID) }
                         select new {PlayerName = pl.Forename, Position = pos.Name};

i am aware now that i am unable to use pl within the right hand side of the join equals, and cannot use pos within the left hand side, is there a way to perform this particular join with linq?

You basically can do this using SelectMany :

var m_player_positions =
    tfs.Players.SelectMany(
                    pl => tfs.Positions
                             .Where(pos => pl.MainPositionID != pos.PositionID)
                             .Select(pos => new {PlayerName = pl.Forename, Position = pos.Name}));

I was beaten to it, but here you go.

  var m_player_positions = from pl in tfs.Players
                         join pos in tfs.Positions
                             on pl.MainPositionID == pos.PositionID
                         select new {PlayerName = pl.Forename, Position = pos.Name};

The restrictions on 'equals' were chosen to ensure that only equalities are used in joins.This is because queries with more advanced logic cannot translate reliably into relational statements.

Consider the 2nd paragraph of this documentation. http://msdn.microsoft.com/en-us/library/bb311040.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM