CMP*_*rez 6 c# linq resharper visual-studio-2008
首先,对于过分的通用名称类抱歉.我的雇主是偏执狂,我肯定知道他漫游这个网站.好的,所以我有这个代码:
var fooObj = new MyFooClass()
{
fooField1 = MyEnum.Value3,
fooField2 = false,
fooField3 = false,
fooField4 = otherEntity.OneCollection.ElementAt(0) as MyBarClass
}
Run Code Online (Sandbox Code Playgroud)
其他的Entity.OneCollection是一个ISet.ISet是一个实现IEnumerable的NHibernate集合类.如果我尝试编译此代码,我会收到此错误:
Error 2 The call is ambiguous between the following methods or properties:
'System.Linq.Enumerable.ElementAt<MyFirm.Blah.Blah.ClassyClass> (System.Collections.Generic.IEnumerable<MyFirm.Blah.Blah.ClassyClass>, int)'
and
'System.Linq.Enumerable.ElementAt<MyFirm.Blah.Blah.ClassyClass>(System.Collections.Generic.IEnumerable<MyFirm.Blah.Blah.ClassyClass>, int)'
Run Code Online (Sandbox Code Playgroud)
但是,如果我在类的开头删除使用System.Linq并将代码更改为:
var fooObj = new MyFooClass()
{
fooField1 = MyEnum.Value3,
fooField2 = false,
fooField3 = false,
fooField4 = System.Linq.Enumerable
.ElementAt(otherEntity.OneCollection, 0) as MyBarClass
}
Run Code Online (Sandbox Code Playgroud)
它编译和工作.(?运算符检查OneCollection是否为了清晰起见而删除了元素)
任何人都可以向我解释这个错误吗?
如果相关:我正在使用Visual Studio 2008,目标是.NET Framework 3.5并使用ReSharper 5.1.
注意 - 编辑以澄清哪个具体的IEnumerable是集合,对不起.
要消除不明确的引用,您必须更加明确。有两种选择。
首先,您可以使 IEnumerable 变得通用,例如:
IEnumerable<MyFooClass> = new List<MyFooClass>();
...
var fooObj = new MyFooClass()
{
fooField1 = MyEnum.Value3,
fooField2 = false,
fooField3 = false,
fooField4 = otherEntity.OneCollection.ElementAt(0) as MyBarClass
}
Run Code Online (Sandbox Code Playgroud)
或者第二;如果它是非泛型 IEnumerable,则进行强制转换:
IEnumerable = new List<MyFooClass>();
...
var fooObj = new MyFooClass()
{
fooField1 = MyEnum.Value3,
fooField2 = false,
fooField3 = false,
fooField4 = otherEntity.OneCollection.Cast<MyFooClass>().ElementAt(0)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3191 次 |
最近记录: |