0

Ok, I'm new to Linq and I'm using VB.NET. Given a list of objects that has 2 properties called AttributeVariable and AttributeValue, I want to select the AttributeValue for the first item in a collection that has a specific AttributeVariable value. This is my start:

Dim query = From c In items 
                 Where c.AttributeVariable = "thename" 
                 Select c.AttributeValue

Cool, it works and I can for each over the query results and write out the result.

Since c.AttributeValue is a String, what is the simplest way to assign the first item in the list (there is only one) to a string variable?

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • See `FirstOrDefault`? Anyway, you can do lots of things .. like those found in the [`Enumerable` extension methods](http://msdn.microsoft.com/en-us/library/system.linq.enumerable(v=vs.110).aspx). – user2864740 Dec 04 '13 at 23:29
  • @user2864740, correct for method syntax but they're using query syntax. – Ash Burlaczenko Dec 04 '13 at 23:30
  • @AshBurlaczenko The query syntax is just a pretty wrapper: [`(someQueryThatResultsInAnIEnumerable).FirstOrDefault()`](http://stackoverflow.com/questions/8886796/linq-firstordefault) will work just fine. Then it's just a matter of how/if that translate into query syntax (which is just transformed back into method calls by the compiler). I don't even know how to write it in *just* query syntax, which is fine because it ultimately doesn't matter. – user2864740 Dec 04 '13 at 23:31
  • (BTW, I down-voted for the title - make sure the title summarizes the *actual* problem/question. While the basic operation of an Enumerable is "iteration over", this can lead to many different queries and transforms: filtering, mapping, grouping, skipping, aggregating, joining .. or even just obtaining the first value, should it exist.) – user2864740 Dec 04 '13 at 23:48

1 Answers1

0

How about FirstOrDefault? (There is also SingleOrDefault and overloads that take default values; consult the documentation.)

Returns the first element of a sequence, or a default value if the sequence contains no elements.

Then:

Dim attribute = (From c In items 
                 Where c.AttributeVariable = "thename" 
                 Select c.AttributeValue).FirstOrDefault()

The key to this is the attribute value (a String) is selected before the FirstOrDefault - thus the resulting type of the expression is a String.

In any case, while I don't believe it is possible to do this with just query syntax, that does not pose an issue because the (query) returns an IEnumerable which can then be used with a "normal" Enumerable extension method as shown.

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • They key for me was actually the () around the LINQ statements to get the FirstOrDefault to be available. I like the title change, point well taken, I'll do better in the future. Thanks! – user3067981 Dec 05 '13 at 02:24
  • @user3067981 Glad this answer was useful - the syntax (especially when combining forms) can be a little hairy. I normally don't use the query syntax at all. – user2864740 Dec 05 '13 at 02:31