views:

497

answers:

2

Here is the single line from one of my functions to test if any objects in my array have a given property with a matching value

Return ((From tag In DataCache.Tags Where (tag.FldTag = strtagname) Select tag).Count = 1)

WHERE....

DataCache.Tags is an array of custom objects

strtagname = "brazil"

and brazil is definately a tag name stored within one of the custom objects in the array.

However the function continually returns false.

Can someone confirm to me that the above should or should not work.

and if it wont work can someone tell me the best way to test if any of the objects in ther array contain a property with a specific value.

I suppose in summary I am looking for the equivalent of a SQL EXISTS statment.

Many thanks in hope.

SJB

+3  A: 

Your code is currently checking whether the count is exactly one.

The equivalent of EXISTS in LINQ is Any. You want something like:

Return DataCache.Tags.Any(Function(tag) tag.FldTag = strtagname)

(Miraculously it looks like that syntax may be about right... it looks like the docs examples...)

Jon Skeet
jon skeet - how many emails do you get from fanboys on this site?
@theman: Not a lot, actually. A few mails from people I knew beforehand, but not a lot.
Jon Skeet
A: 

Many Thanks for the response.

Your code did not work. Then I realised that I was comparing to an array value so it would be case sensitive.

However glad I asked the question, as I found a better way than mine.

Many thanks again !