SJB101
SJB101

Reputation:

Newbie issue with LINQ in vb.net

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 definitely 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 the array contain a property with a specific value.

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

Many thanks in hope.

Upvotes: 2

Views: 3644

Answers (2)

SJB101
SJB101

Reputation:

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 !

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500065

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...)

Upvotes: 6

Related Questions