score:5

Accepted answer

When you apply Where/Select to Temp, you're not just getting a single result (there could be more than one result matching where item.GlassesID==GlassID, the compiler cannot be sure) so it does not return a single object instance of Glasses but an IEnumerable<Glasses>. Simplified, think that you get a List<Glasses> back and you need to extract the actual object from the List before using its properties.

If you definitely know there's at most only one result returned, you can use SingleOrDefault() which returns the Glasses instance in the IEnumerable or null if it's empty, or Single() which returns the Glasses instance in the IEnumerable and throws an error if there isn't one. Both throw an exception if there is more than one result.

Using First() or FirstOrDefault() would mean that if you accidentally get more than one result, you'll in this case get the first one, a random one. Perhaps not a good thing for an "itemToDelete", you'd probably want to know if your assumption is wrong.

score:5

BTW, you can hover over var in Visual Studio to see the type of an expression.

In this case, itemToDelete is an IEnumerable<Glasses> of Glasses in Temp with a GlassesID of GlassID. Assuming your GlassesID is actually unique in Temp, it will only have one item which you can extract with .Single() or .SingleOrDefault().

EDIT: As others have pointed out, .Single(), not .First() is the right method to use if you want to check for exactly one item.

score:3

LINQ queries always produce a seqence of items, of type IEnumerable<Glasses> in your case. Even if there is only one element in the sequence, it is still a sequence. The sequence itself can only be enumerated, it does not have any other properties. To get at the elements' properties you first have to extract an element. That's exactly what First() does. It returns the first element of the sequence.

If your query always should produce exactly one item and any other result is an error you should use Single() instead of First(). If zero or one elements are acceptable you can use SingleOrDefault() and check the result for null.

score:2

Because your first version is a collection of Glasses, not a Glasses object itself. First() or FirstOrDefault() return a single instance of Glasses, thus the properties are available.

score:1

as the first() will give only a single item. so the compiler is giving intellisense for it. as the 1st version is IEnumerable<Glasses> so it has collection of objects. you need to iterate it.

 var itemToDelete = (from item in Temp
                       where item.GlassesID==GlassID
                       select item);     

foreach(var i in itemToDelete )
{
   i.SomeProperty // do some thing with i ;
}

Related Articles