【问题标题】:Getting wrong output with linq join query使用 linq 连接查询得到错误的输出
【发布时间】:2016-04-30 05:29:59
【问题描述】:

我有这些数据:用户

UserId   Name
 42      Abc  
 43      Pqr
 44      lmn
 45      xyz

映射

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

我想获取所有不在user 1 中的user2 用户,因此考虑到上述输入,输出将低于:

预期输出

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

注意:没有外键关系,因此没有导航属性。

【问题讨论】:

    标签: c# .net linq join


    【解决方案1】:

    这是您在控制台应用程序中遇到的问题的解决方案。我用 where !(..) 来查找那些不在映射 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
    

    【讨论】:

      【解决方案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();
      

      【讨论】:

      • 2次获取userid 45。
      • @Learning 添加了.Distinct() 来解决这个问题
      • 非常感谢您的回答。它正在工作。非常感谢您
      【解决方案3】:

      试试这个:

      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();
      

      【讨论】:

      • 非常感谢您的回答。它正在工作。非常感谢您
      【解决方案4】:

      你可以试试这个。

      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。 因此,如果此解决方案有效,您可以自己修改新对象。

      【讨论】:

      • 请通过选择您的代码格式化您的代码,然后按 ctrl + k.Welcome to stackoverflow
      • 感谢您给我建议。
      【解决方案5】:

      试试这段代码。

      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();
      

      【讨论】:

      • 非常感谢您的回答。它正在工作。非常感谢您
      • @Learning,我认为这个答案是错误的,试试这个sample
      • @Slava Utesinov 修复它
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 1970-01-01
      相关资源
      最近更新 更多