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

发布于 2024-09-08 05:02:41 字数 810 浏览 2 评论 0原文

我有以下方法签名:

 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;

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

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

所以我认为我需要一个 lambda 表达式,但我很困惑这会如何被建造。

我认为我正朝着正确的方向前进,但有人可以帮助我克服最后的障碍吗?

I have the following method signature:

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

PrimaryKeyData is a class that has ServerID and LocalID integers as properties.
Student is a class that (among other properties) has an integer one called StudentID.

In English, what I want to do is return an array of integers which are in existingStudents[...].ServerID but not in linkedStudents[...].StudentID

If 'existingStudents' and 'linkedStudents' were both integer arrays, I would use a linq query as below:

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

..which could then be converted to an array of ints.

What I want to do is give Contains an IEqualityOperator that will consider a PrimaryKeyData class to be equal to a Student class if PrimaryKeyData.ServerID == Student.StudentID

So I think I need a lambda expression but I'm very confused on how that would be constructed.

I think I'm going in the right direction but can anyone help me over the final hurdle?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟─花易冷 2024-09-15 05:02:41

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

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

So, my understanding is that you want to get all instances of PrimaryKeyDataV1 whose ServerID property doesn't exist in any of the students.StudentID property of the linkedStudents parameter?

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

Or if you just want an array of IDs...

internal static int[] GetStudentIDsThatAreNotLinked(PrimaryKeyDataV1[] existingStudents, IQueryable<Student> linkedStudents)
{
    return existingStudents.Select(s => s.ServerID)
        .Except(linkedStudents.Select(link => link.StudentID))
        .ToArray();
}
打小就很酷 2024-09-15 05:02:41

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

If you only need to return the IDs, you can use something like:

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

You can write this in query comprehension form, but I think it's less clear:

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

If you need to return the result as an array, just use the .ToArray() extension:

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

You don't need to create your own equality comparer in the case that you only need to return the set difference in IDs.

酷炫老祖宗 2024-09-15 05:02:41
    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;
}
    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));
    }

simple student class:

public class student
{
    public int id;
    public string name;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文