在.Net中返回类型的匿名方法

时间:2010-02-03 11:55:41

标签: c# .net vb.net anonymous

这真是我的好奇心的问题,因为我知道还有其他方法可以解决这个问题。

我的“Item”类 - “MyProperty”上有一个属性 - 我想评估。我想遍历集合 - “MyItemCollection” - ,如果有一个“Item”类属性“MyProperty”不是什么我想要设置一个布尔标志来指示该集合包含任何一个非null MyProperty它的“物品”物品。

Private ContainsPOF = Function() (From thisItem As Item In MyItemCollection Where Item.MyProperty IsNot Nothing Select item).Count > 0

这给了我一个警告:“没有'As'子句的变量decleration;假定的对象类型”,所以我尝试了

Private ContainsPOF As Boolean = Function() (From thisItem As Item In MyItemCollection Where Item.MyProperty IsNot Nothing Select item).Count > 0 subc).Count > 0

然而,这给了我错误“Lamda表达式无法转换为'Boolean'因为'Boolean'不是委托类型”

有没有让这个Function类型的返回安全,或者我应该使用不同的方法(旧式函数)?

感谢。

2 个答案:

答案 0 :(得分:3)

我认为您要将其声明为Func(Of Boolean)

Private ContainsPOF As Func(Of Boolean) = [...]

据我所知,虽然没有创建属性(根据你的第一段)。为什么不将它声明为普通属性?

答案 1 :(得分:0)

这可能更清晰,因为linq表达式匹配集合中非空的任何项目,例如:

ContainsPDF = ThisCollection.Any(x=>x.MyClass IsNot Nothing);