迭代集合项并检查每个属性的有效值
我有一个对象集合(IQueryable)。每个对象都有各种属性,一些字符串一些日期时间,我不关心日期时间属性。如何迭代每个对象并返回在一个或多个字段中可能具有空值的对象的集合
为简单起见,请考虑一组员工
每个员工可能有两个属性: 名字(字符串) LastName (string)
我想要一个方法,可以迭代员工集合中的所有员工,并返回名字或姓氏缺失的员工集合,即 null 或空字符串。
将 .NET 3.5 与 C# 结合使用
I have a collection of objects (IQueryable). Each object has various properties, some string some datetime, I'm not concerned about the datetime properties. How can I iterate through each object and return a collection of those objects that maybe have null values in one or more fields
For simplicity, consider a collection of Employees
Each employee may have two properties:
FirstName (string)
LastName (string)
I'd like to have a method that could iterate through all employees in the employee collection and return a collection of employees that either have first name or last name missing, i.e null or an empty string.
using .NET 3.5 with C#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这应该可以帮助您开始:
很清楚如何使用泛型来概括这一点。
更好的是只拥有一个实现某些接口
ISpecification
的具体类(明显的接口方法是bool IsSatisfiedBy(T实体)
),然后例如:
然后你可以说:
This should get you started:
It's clear how to generalize this using generics.
Even better is to just have a concrete class that implements some interface
ISpecification<T>
(obvious interface method isbool IsSatisfiedBy(T entity)
) and thenFor example:
Then you can say:
只需创建第二个集合“invalidEmployees”
即可迭代 Employee 集合。检查当前 Employee 是否有空值(或任何其他特征),然后将它们添加到“invalidEmployees”集合中。
Simply create a second collection "invalidEmployees"
Iterate through Employee collection. Check current Employee for null values (or any other characteristic) then add them to the "invalidEmployees" collection.
如果我理解这个问题,那么解决方案应该非常简单:
其中 iQuaryable 是 IQuaryable 实现的实例。
If I understand the question, the solution should be pretty trivial:
where the iQuaryable is the instance of your IQuaryable implementation.
您想明确说明您的方法将检查对象的哪些字段吗?
或者您想要检查所有字符串属性吗?
或者属性是静态的并且在编译时已知?
这些对象都是同一类型吗?
它可以像其他答案中所示的 where 子句一样简单:
或者它可以像指定要检查的字符串属性名称并迭代它们一样复杂:
可使用类似以下内容调用:
您可以进一步减少属性数量您检查是否预过滤字符串类型。在 GetProperties() 之后添加
Do you want to explicitly state which fields for which your method will check the objects?
Or do you want all string properties checked?
Or are the properties static and known at compile time?
Are the objects all the same type?
It could be as simple as a where clause like shown in other answers:
Or it could be as complex as specifying the string property names you want to check and iterating them:
Callable with something like:
You could further cut down on the number of properties you check against if you pre-filter for string types. After the GetProperties() add