对对象数组执行 linq 查询?

发布于 2024-10-21 14:18:40 字数 279 浏览 2 评论 0原文

我有一个类型对象的集合,我知道它们是 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 技术交流群。

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

发布评论

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

评论(5

我一直都在从未离去 2024-10-28 14:18:40

您可以使用其中一个

from e in m_Employees.Cast<Employee>()
    select e.DepartmentCode

,或者

from e in m_Employees.OfType<Employee>()
    select e.DepartmentCode

如果您无法将每个项目强制转换为 EmployeeCast 会抛出错误,但 OfType 将过滤掉那些与类型。

You can use either

from e in m_Employees.Cast<Employee>()
    select e.DepartmentCode

or

from e in m_Employees.OfType<Employee>()
    select e.DepartmentCode

Cast thows an error if you can not cast each item to Employee, but OfType will filter out those objects not matching the type.

望笑 2024-10-28 14:18:40

与其使用 as Employee ,不如让编译器基本上使用显式类型范围变量插入对 Cast 的调用:

var options = (from Employee e in m_Employees
               select e.DepartmentCode).Distinct();

或者替代地和等效地:

var options = m_Employees.Cast<Employee>()
                         .Select(e => e.DepartmentCode)
                         .Disinct();

但是,如果数组确实确实仅包含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 to Cast<T> using an explicitly typed range variable:

var options = (from Employee e in m_Employees
               select e.DepartmentCode).Distinct();

Or alternatively and equivalently:

var options = m_Employees.Cast<Employee>()
                         .Select(e => e.DepartmentCode)
                         .Disinct();

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 a NullReferenceException 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 a NullReferenceException or an InvalidCastException.

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>() and OfType<T>() are both extension methods on just IEnumerable instead of on IEnumerable<T>.

如梦 2024-10-28 14:18:40

一种选择是:

(from e in m_Employees
let x = e as Employee
select x.DepartmentCode).Distinct();

One option would be:

(from e in m_Employees
let x = e as Employee
select x.DepartmentCode).Distinct();
蓬勃野心 2024-10-28 14:18:40

使用这个:

var options = (from e in m_Employees.Cast<Employee>()
               select e.DepartmentCode).Distinct();

重要的部分是Cast。我的答案假设 m_Employees 是一个 IEnumerable (如 ArrayList),而不是一个 IEnumerable (如 List<) ;员工>

Use this:

var options = (from e in m_Employees.Cast<Employee>()
               select e.DepartmentCode).Distinct();

The important part is the Cast<Employee>. My answer assumes that m_Employees is an IEnumerable like ArrayList instead of an IEnumerable<Employee> like List<Employee>.

π浅易 2024-10-28 14:18:40

你可以试试这个:

var options = m_Employees
        .Cast<Employee>()
        .Select(item => item.DepartmentCode)
        .Distinct();

You could try this:

var options = m_Employees
        .Cast<Employee>()
        .Select(item => item.DepartmentCode)
        .Distinct();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文