2016-04-30 19 views
1

我有這樣的數據:用戶獲取輸出錯誤使用LINQ連接查詢

UserId Name 
42  Abc 
43  Pqr 
44  lmn 
45  xyz 

映射

MappingId User1 User2 
1   42 43 
2   42 44 
3   43 44 

我想所有的用戶是user2這是不是在user 1所以輸出將在以下考慮以上輸入:

期望輸出

UserId Name 
44  lmn 

這是我的查詢:

var data = (from user in context.Users 
          join mappng in context.Mappings on user.UserId equals mappng.User2 
          where mappng.User1 != mappng.User2 
          select new 
          { 
           Name = user.FirstName + " " + user.LastName, 
           UserId = user.UserId, 
          }).ToList(); 

但是,得到的輸出錯誤:

UserId Name 
43  Pqr 
44  lmn 
44  lmn 

任何人可以幫助我???

備註沒有外鍵關係,所以沒有導航屬性

回答

3

下面是在控制檯應用程序中解決問題的方法。我用哪裏!(..)找到那些不在映射User1中。我不確定這種方法的其他替代方案。但希望它有幫助。

class Program 
{ 
    static void Main(string[] args) 
    { 
     List<User> users = new List<User>() 
     { 
      new User() { UserId = 42, Name = "Abc" }, 
      new User() { UserId = 43, Name = "Pqr" }, 
      new User() { UserId = 44, Name = "lmn" }, 
      new User() { UserId = 45, Name = "xyz" }, 
     }; 

     List<UserMapping> userMappings = new List<UserMapping>() 
     { 
      new UserMapping() { MappingId = 1, User1 = 42, User2 = 43}, 
      new UserMapping() { MappingId = 2, User1 = 42, User2 = 44}, 
      new UserMapping() { MappingId = 3, User1 = 43, User2 = 44}, 
     }; 

     var data = (from u in users 
         join m in userMappings on u.UserId equals m.User2 
         where !(from mm in userMappings 
           select mm.User1).Contains(u.UserId) 
         select u).Distinct(); 

     foreach(var entry in data) 
     { 
      Console.WriteLine(entry.UserId + " " + entry.Name); 
     } 

     Console.ReadLine(); 
    } 
} 

class User 
{ 
    public int UserId { get; set; } 
    public string Name { get; set; } 
} 

class UserMapping 
{ 
    public int MappingId { get; set; } 
    public int User1 { get; set; } 
    public int User2 { get; set; } 
} 

輸出:

44 lmn 
+0

非常感謝你的回答。它工作 –

2

的LINQ張貼由使用mappng.User1 != mappng.User2,這是不想要的查詢進行比較同一行的User1User2值。嘗試使用!Any()如下:

var data = (from user in context.Users 
      join mappng in context.Mappings on user.UserId equals mappng.User2 
      where !context.Mappings.Any(m => m.User1 == user.UserId) 
      select new 
      { 
       Name = user.FirstName + " " + user.LastName, 
       UserId = user.UserId, 
      }).Distinct().ToList(); 
+0

獲得userid 45 2次。 –

+1

@學習添加'.Distinct()'來修復 – har07

+0

我真的很感謝你的答案。它是工作。非常感謝你 –

2

試試這個:

var data = (from map1 in context.Mappings 
      join map2 in context.Mappings 
      on map1.User2 equals map2.User1 into subMap 
      from sub in subMap.DefaultIfEmpty() 
      where sub == null 
      join user in context.Users on map1.User2 equals user.UserId 
      select new { 
       Name = user.FirstName + " " + user.LastName, 
       user.UserId, 
      }).Distinct().ToList(); 
+0

我真的很感謝你的答案。它是工作。非常感謝你 –

2

試試這個代碼剪斷。

var result = context.Mappings.Where(mapping1 => !context.Mappings.Select(mapping2 => mapping2.User1).Contains(mapping1.User2)) 
      .Select(e=> e.User2).Distinct() 
      .Join(context.Users, arg => arg, user=> user.UserId,(arg,user) => user) 
      .ToList(); 
+0

我真的很感謝你的答案。這是工作。謝謝你這麼多 –

+1

@Learning,我認爲,這個答案是錯誤的,試試這個[示例](https://dotnetfiddle.net/lc5CGA) –

+0

@Slava Utesinov修復它 –

2

你可以試試這個。

var data = (select user from context.Users where (from m2 in context.Mappings select m2.User2).Except(from m1 in context.Mappings select m1.User1).Contains(user.UserId) select new {Name=user.Name, UserId=user.UserId}).ToList(); 

我在示例表中看不到user.FirstName,user.LastName。 因此,如果此解決方案正常工作,您可以自行修改新對象。

+0

請格式化您的代碼,通過選擇你的代碼並按下ctrl + k。歡迎使用stackoverflow –

+1

感謝您向我提供建議。 – user3207464