On Thu, 21 Aug 2008 18:17:33 -0700, shapper <md*****@gmail.comwrote:
Hello,
I have the following Linq query:
(from t in database.Tags
where t.Name.ToLower == tag
select t)
I want to define a variable, named Found, that is true if any record
is found or false is no record is found.
What is the best way to do this?
First, I'm going to assume that "ToLower" is some special property you've
defined on whatever class it is that's returned by the Name property of
your "t" element type. If Name is actually just a String, you probably
want to use "t.Name.ToLower()" :).
Second, the question on the face of it seems sort of, um...pointless.
That is, why would you need a flag? If all you want is _just_ the flag,
the LINQ query is superfluous. Just search the original collection.
Depending on its type, it might even have the Exists() method, but even if
not, you can use the LINQ Enumerable.Any() overload that takes a predicate
to test for existence.
On the other hand, if you really need the LINQ query, as implied by your
question, then it's not clear why you need the flag at all. Assuming you
do need it _somewhere_, why you can't just set it during the course of
enumerating the results (initialize to false, set to true if you actually
wind up finding an element in the results).
Now, all that said, if the above all seems irrelevant and not useful, you
can simply use the Enumerable.Any() overload that has no parameters. It
will return true if the collection is non-empty, false if it's empty.
Just use it on your query results to assign your flag.
Hope that helps. :)
Pete