score:5
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: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 ;
}
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: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: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.
Source: stackoverflow.com
Related Query
- IntelliSense dosen't give me option to select properties after implementing the LINQ
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Implementing Linqs Select without the yield keyword. Can't follow the control flow
- How do I select everything in a list after the first occurence of?
- How to compare two lists on a combination of two properties and select a row which has mismatch in the third property?
- Select items from a list after the first occurrence of a character
- How do I select everything after the last instance of?
- C# LINQ - Select statement that compares all properties of a class with a different instance of the same class?
- How do I write a linq select statement without knowing at design time the names of the properties I'm going to select?
- Is possible keep the properties type after using linq selection?
- Select the next row after nth duplicate rows
- Extension of IEnumerable's Select to include the source in the selector
- How can i use the select method for Multiple properties with same property name
- LINQ: Select the Min and Max values from a collection on an entity after grouping
- How to write aggregate query in LINQ reusing part of the select code
- Why are the properties of my partial class empty after I've run my LINQ query?
- Linq Select items where they are before a certain date or the first after
- EF. Select records from one table based on the properties of the records from second table that have an FK from the first
- Why cant we select any properties on the many side when we are using .Select(x=>new object) on Linq
- How to select from the list only some properties depending from list of property names?
- How to convert a string to C# code in the SELECT of C# LINQ
- how to select properties in the order from Object using LINQ?
- linq after the join groupby and select
- EF Core LINQ GROUPBY Then Select to get more than one properties of the entity
- How to get the properties with name, old value and new value after comparing two class objects
- How to send a single json response from a vb.net linq query using the select option
- How to select list class properties on the base max value in each group?
- How to select the properties to be filled instead of all properties of the object
- LINQ: Select an object and change some properties without creating a new object
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
More Query from same tag
- Selecting a new object without grouping in linq?
- Return array from Linq SQL query on 80 million row table using EFCore
- Differences between LINQ to Objects and LINQ to SQL queries
- Filter some unique Data with LINQ and C#
- Binding Grid to a Linq Datasource from Winforms (.net)
- Using Expression Class With LINQ Queries
- LINQ/LAMBDA filter query by date
- Counting Column Rows using return JSON and Linq on google charts
- I want to fetch Year from x.CreatedDate how to do that?
- How can I write an UPDATE in LINQ without fetching the object, changing it, and calling SaveChanges?
- Test whether two IEnumerable<T> have the same values with the same frequencies
- Return max repeated item in list
- Returning single value from SQL Stored Procedure using LINQ
- Linq collection with not like?
- LINQ to XML Newbie Question
- LINQ get last position of special char
- I've just upgraded from a VS2010 project, but my linq query now won't sum an anonymous type
- How to use T-SQL timestamp with Linq to Entities EF4
- CRM 2011 - N:N (Many-To-Many) Linq Issue
- How to write an expression for string concatenation for linq?
- Flattening a tree of classes using SelectMany()
- Converting a list of lists into a single list using linq
- Make Linq return object or null
- Operator '!=' cannot be applied to operands of type 'bool?' and 'int'
- C# Linq Groupby and Where
- How to skip(m).take(n) from a List<T>?
- NHibernate, expression trees, and eliminating repetition
- Linq with Aggregate inside Join
- This is a linq sanity check
- Linq Take and Ria Services