代码之家  ›  专栏  ›  技术社区  ›  Neil Trodden

如何使用linq返回一个数组中与另一个数组的整数属性不匹配的整数?

  •  6
  • Neil Trodden  · 技术社区  · 14 年前

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

    PrimaryKeyData是一个具有ServerID和LocalID整数作为属性的类。

    在英语中,我要做的是返回一个整数数组,该数组位于existingStudents[…].ServerID中,但不在linkedStudents[…].StudentID中

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

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

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

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

    我想我走的方向是对的,但是有人能帮我跨过最后一关吗?

    3 回复  |  直到 14 年前
        1
  •  7
  •   Matthew Abbott    14 年前

    所以,我的理解是,您希望获取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));
    }
    

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

    如果您只需要返回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中的设置差异,则不需要创建自己的相等比较器。

        3
  •  0
  •   Robaticus    14 年前
        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;
    }