Call is ambiguous error using Linq

狂风中的少年 提交于 2019-12-01 04:47:42

This is very strange.

The error claims that there's an ambiguity between two completely identical signatures.

The only thing I can think of is that you might somehow have the 'references' messed up in your project and that maybe you have i.e. reference both to 'System.Core 3.5' and 'System.Core 3.5.0.1' (contrived example).. You would get such error in this case, but yet, I cannot think why System.Linq.Enumerable.ElementAt(otherEntity.OneCollection works - it should report the same problem.

.. or maybe you somehow made your 'OneCollection' implement the IEnumerable twice in some wicked way? But then, I think the error message would be different.

To remove the ambiguous reference, you have to be more explicit. There are two options.

First, you could make your IEnumerable generic, like:

IEnumerable<MyFooClass> = new List<MyFooClass>();
...
var fooObj = new MyFooClass()
{
     fooField1 = MyEnum.Value3, 
     fooField2 = false,
     fooField3 = false,
     fooField4 = otherEntity.OneCollection.ElementAt(0) as MyBarClass
 }

Or second; if it's a non-generic IEnumerable, do a cast:

IEnumerable = new List<MyFooClass>();
...
var fooObj = new MyFooClass()
{
     fooField1 = MyEnum.Value3, 
     fooField2 = false,
     fooField3 = false,
     fooField4 = otherEntity.OneCollection.Cast<MyFooClass>().ElementAt(0)
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!