对对象数组执行 linq 查询?
我有一个类型对象的集合,我知道它们是 Employee 类型。我想使用员工对它们执行一些与此类似的 linq 操作:
var options = (from e in m_Employees
select (e as Employee).DepartmentCode).Distinct();
但是 as 员工给出错误并不奇怪。有办法解决吗?
更改集合并不是真正的选择,因为我正在维护代码并且我想避免大的更改。
I have a collection of type objects which I know they are type Employee. I'd like to perform some linq operations on them using the employee some thing similar to this:
var options = (from e in m_Employees
select (e as Employee).DepartmentCode).Distinct();
But the as employee is not surprisingly giving an error. is there a way around it?
Changing the collection is not really an options since I,m maintaining the code and I want to avoid big changes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用其中一个
,或者
如果您无法将每个项目强制转换为
Employee
,Cast
会抛出错误,但OfType
将过滤掉那些与类型。You can use either
or
Cast
thows an error if you can not cast each item toEmployee
, butOfType
will filter out those objects not matching the type.与其使用
as Employee
,不如让编译器基本上使用显式类型范围变量插入对Cast
的调用:或者替代地和等效地:
但是,如果数组确实确实仅包含
Employee
引用,我仍然不会期望您的原始代码会失败...如果您收到NullReferenceException 则或者其中一个值为 null,或者它是对非 Employee 对象的非 null 引用。上述代码仍然会给您带来错误,但您可以根据是否仍然收到
NullReferenceException
还是InvalidCastException
来判断是哪一个错误。一般来说,当您打算有条件地使用结果时,您应该只使用
as
。如果您确定每个值确实是正确的类型,那么您应该使用强制转换 - 这样,如果您错了,您将得到代码,并出现异常,而不是将空引用传播到代码的其余部分,这可能会在以后造成损害并使得很难发现错误的根源。如果您收到编译时错误,则根据您看到的异常,有多种可能的原因。
编辑:好的,所以这是一个
IEnumerable
导致编译时错误...Cast()
和OfType()
code> 都是IEnumerable
上的扩展方法,而不是IEnumerable
上的扩展方法。Rather than using
as Employee
it would be better to make the compiler basically insert a call toCast<T>
using an explicitly typed range variable:Or alternatively and equivalently:
However, I still wouldn't have expected your original code to fail, if the array really does only include
Employee
references... If you were getting aNullReferenceException
then either one of the values was null, or it was a non-null reference to a non-Employee object. These will both still give you an error with the above code, but you'll be able to see which one based on whether you still get aNullReferenceException
or anInvalidCastException
.In general you should only use
as
when you're going to use the result conditionally. If you're sure that every value is really of the right type, you should use a cast instead - so that if you're wrong, you'll get the code blowing up with an exception instead of propagating a null reference to the rest of the code, where it could cause harm later on and make it hard to spot the source of the error.If you were getting a compile-time error then there are a number of possible causes, based on what exception you were seeing.
EDIT: Okay, so it was an
IEnumerable
causing a compile-time error...Cast<T>()
andOfType<T>()
are both extension methods on justIEnumerable
instead of onIEnumerable<T>
.一种选择是:
One option would be:
使用这个:
重要的部分是
Cast
。我的答案假设m_Employees
是一个IEnumerable
(如ArrayList
),而不是一个IEnumerable
(如List<) ;员工>
。Use this:
The important part is the
Cast<Employee>
. My answer assumes thatm_Employees
is anIEnumerable
likeArrayList
instead of anIEnumerable<Employee>
likeList<Employee>
.你可以试试这个:
You could try this: