如何使用 linq 返回一个数组中与另一个数组的整数属性不匹配的整数?
我有以下方法签名:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以,我的理解是,您想要获取其 ServerID 属性不存在于 linkedStudents 参数的任何 Students.StudentID 属性中的 PrimaryKeyDataV1 的所有实例?
或者如果你只想要一个 ID 数组......
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?
Or if you just want an array of IDs...
如果您只需要返回 ID,您可以使用类似的内容:
您可以以查询理解形式编写此内容,但我认为不太清楚:
如果您需要将结果作为数组返回,只需使用
.ToArray ()
扩展:如果您只需要返回 ID 中设置的差异,则无需创建自己的相等比较器。
If you only need to return the IDs, you can use something like:
You can write this in query comprehension form, but I think it's less clear:
If you need to return the result as an array, just use the
.ToArray()
extension:You don't need to create your own equality comparer in the case that you only need to return the set difference in IDs.
简单学生班:
simple student class: