的SelectMany()不能推断类型参数 - 为什么不呢? [英] SelectMany() Cannot Infer Type Argument -- Why Not?

查看:488
本文介绍了的SelectMany()不能推断类型参数 - 为什么不呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个员工表和办公室表。这些都是加入了通过 EmployeeOffices 表中的多对一对多的关系。

I have an Employee table and an Office table. These are joined in a many-to-many relationship via the EmployeeOffices table.

我想获得特定员工( CurrentEmployee )与相关联的所有办事处的列表。

I'd like to get a list of all the offices a particular employee (CurrentEmployee) is associated with.

我想我能做些什么像这样的:

I thought I could do something like this:

foreach (var office in CurrentEmployee.EmployeeOffices.SelectMany(eo => eo.Office))
    ;



但是,这给我的错误:

But this gives me the error:

有关方法'System.Linq.Enumerable.SelectMany(System.Collections.Generic.IEnumerable,System.Func>)'的类型参数不能从使用推断。尝试显式指定类型参数。

The type arguments for method 'System.Linq.Enumerable.SelectMany(System.Collections.Generic.IEnumerable, System.Func>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

我明白我可以添加类型参数。但是,智能感知认识到 eo.Office 的类型是办公室。那么,为什么不明确告知编译器?

I understand I could add type arguments. But Intellisense recognizes that eo.Office is of type Office. So why isn't this clear to the compiler?

推荐答案

由您传递给委托返回类型的SelectMany 必须是的IEnumerable< TResult> ,但显然,办公室不实现该接口。它看起来像你只是混淆的SelectMany 很简单选择方法。

The type returned by the delegate you pass to SelectMany must be an IEnumerable<TResult>, but evidently, Office doesn't implement that interface. It looks like you've simply confused SelectMany for the simple Select method.


  • 的SelectMany 是压扁多套到一个新的集。

  • 选择 是一个一对一的映射在源的每个元素设为新集

  • SelectMany is for flattening multiple sets into a new set.
  • Select is for one-to-one mapping each element in a source set to a new set.

我觉得这是你想要什么:

I think this is what you want:

foreach (var office in CurrentEmployee.EmployeeOffices.Select(eo => eo.Office))

这篇关于的SelectMany()不能推断类型参数 - 为什么不呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆