LINQ queries return enumerable's for the items in the query. Its not until you start going through the collection that you will get an item. In order to return a strongly typed item, you should do something like this:
Dim UpdatePurchase As Purchase = (From purchased In ThisCustomer.Purchases _
Where purchased.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString) _
Select purchased).FirstOrDefault
And, looking at the error you got, we can determine that the LINQ query is returning an Enumerable (the WhereSelectEnumerableIterator`2[TestTestModelFirst.Purchase,TestTestModelFirst.Purchase] basically means
Enumerable(of Purchase)
. You can't cast an enumerator to a type, you have to actually get the first value from the enumerator, which the code above does.
Additionally,
var
is not a type, its an ommission of a type, so you could also write:
Dim UpdatePurchases = From purchased In ThisCustomer.Purchases _
Where purchased.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString) _
Select purchased
Which is the same as using the
var
keyword in C#. Var basically tells the compiler to infer the data type of UpdatePurchases by the value on the right side of the equals, in this case UpdatePurchases would be an
Enumerable(Of Purchase)
Which will now let you enumerate using a ForEach, like:
For Each purchaseItem as Purchase in UpdatePurchases
Next