【问题标题】:How can I use linq to return integers in one array that do not match up with an integer property of another array?如何使用 linq 返回一个数组中与另一个数组的整数属性不匹配的整数?
【发布时间】:2010-07-02 05:03:17
【问题描述】:

我有以下方法签名:

 internal static int[] GetStudentIDsThatAreNotLinked(PrimaryKeyDataV1[]
       existingStudents, IQueryable<Student> linkedStudents)

PrimaryKeyData 是一个具有 ServerID 和 LocalID 整数作为属性的类。 Student 是一个类(在其他属性中)有一个称为 StudentID 的整数。

在英语中,我想要做的是返回一个整数数组,这些整数在 existingStudents[...].ServerID 但不在linkedStudents[...].StudentID 中

如果“existingStudents”和“linkedStudents”都是整数数组,我会使用如下的 linq 查询:

return from es in existingStudents where
    !linkedStudents.Contains<int>(es) select es;

..然后可以转换为整数数组。

我想要做的是给包含一个 IEqualityOperator,如果 PrimaryKeyData.ServerID == Student.StudentID,它将认为 PrimaryKeyData 类等于 Student 类

所以我认为我需要一个 lambda 表达式,但我对如何构造它感到非常困惑。

我认为我的方向是正确的,但谁能帮助我克服最后的障碍?

【问题讨论】:

    标签: c# linq lambda


    【解决方案1】:

    所以,我的理解是,您想要获取 PrimaryKeyDataV1 的所有实例,其 ServerID 属性在linkedStudents 参数的任何 students.StudentID 属性中都不存在?

    internal static PrimaryKeyDataV1[] GetStudentsThatAreNotLinked(PrimaryKeyDataV1[] existingStudents, IQueryable<Student> linkedStudents)
    {
        var results = existingStudents.Select(s => s.ServerID)
            .Except(linkedStudents.Select(link => link.StudentID))
            .ToArray();
    
        return existingStudents.Where(stud => results.Contains(stud.ServerID));
    }
    

    或者,如果您只想要一个 ID 数组...

    internal static int[] GetStudentIDsThatAreNotLinked(PrimaryKeyDataV1[] existingStudents, IQueryable<Student> linkedStudents)
    {
        return existingStudents.Select(s => s.ServerID)
            .Except(linkedStudents.Select(link => link.StudentID))
            .ToArray();
    }
    

    【讨论】:

      【解决方案2】:

      如果您只需要返回 ID,您可以使用类似:

      existingStudents.Select(es => es.StudentID)
                    .Except(linkedStudents.Select(ls => ls.ServerID));
      

      你可以用查询理解的形式写这个,但我认为它不太清楚:

      var result = (from es in existingStudents select es.StudentID);
                   .Except
                   (from ls in linkedStudents select ls.ServerID)
      

      如果您需要将结果作为数组返回,只需使用.ToArray() 扩展名:

      existingStudents.Select(es => es.StudentID)
                    .Except(linkedStudents.Select(ls => ls.ServerID)).ToArray();
      

      如果只需要返回设置的 ID 差异,则无需创建自己的相等比较器。

      【讨论】:

      • 我很欣赏关于查询理解的额外细节,但不幸的是,查询返回的链接学生不在现有学生中,但我需要现有学生不在链接学生中
      • @Neil:抱歉……我会修正我的答案。
      • 感谢并标记。遗憾的是,我们不能接受多个答案,因为两者都非常赚钱。
      【解决方案3】:
          List<student> ExistingStudents = new List<student>();
          List<student> LinkedStudents = new List<student>();
      
          ExistingStudents.Add(new student {id=1, name="joe"});
          ExistingStudents.Add(new student { id = 2, name = "beth" });
          ExistingStudents.Add(new student { id = 3, name = "sam" });
      
          LinkedStudents.Add(new student { id = 2, name = "beth" });
      
      
          var students = from stud in ExistingStudents
                          where !(LinkedStudents.Select(x => x.id).Contains(stud.id))
                          select stud;
      
          foreach(student s in students)
          {
              System.Diagnostics.Debug.WriteLine(string.Format("id: {0}, name: {1}", s.id, s.name));
          }
      

      简单的学生课:

      public class student
      {
          public int id;
          public string name;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-14
        • 2014-10-31
        • 2013-05-31
        相关资源
        最近更新 更多