开发者

Anonymous methods with return type in .Net

开发者 https://www.devze.com 2022-12-19 05:04 出处:网络
This is a question for my curiosity really, as i know there are other ways to work around the problem.

This is a question for my curiosity really, as i know there are other ways to work around the problem.

I have a property on my "Item" class - "MyProperty" - that I want to evaluate. I want to iterate through the collection - "MyItemCollection" - , and if there is an "Item" class whos property "MyProperty" is not nothing i want to set a Boolean flag to indicate that the collection contains a non null MyProperty on any of its "Item" Objects.

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

This gives me a warning of "Variable decleration without an 'As' clause; type of Object assumed", so i tried

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

This however gives me the error of "Lamda expression cannot be converted to 'Boolean' As 'Boolean' is not a delegate type"

Is there anyway to make the return of thi开发者_开发知识库s Function type safe, or should I just use a different method (an old style Function)?

Thanks.


I think you want to declare it as Func(Of Boolean) instead:

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

As far as I can see that's not creating a property (as per your first paragraph) though. Why not declare it as a normal property?


this might be clearer as a linq expression matching any items in the collection that are not null eg:

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

精彩评论

暂无评论...
验证码 换一张
取 消