score:2
ienumerable<element>.cast<someclass>().firstordefault();
is probably want you want, but the answer has nothing to do with performance.
unless element
is ienumerable
, it doesn't make sense to use cast()
on the result of firstordefault()
, and will result in a compile error.
assuming you meant (someclass)(ienumerable<element>.firstordefault());
for your second case, this is also problematic because if ienumerable<element>
is empty, then firstordefault()
will return null
if element
is a reference type, or default construct element
if it's a value type. it would be preferable to default construct someclass
in the event that ienumerable<element>
is empty and someclass
is a value type, rather than attempting to cast from element
to someclass
.
ienumerable<element>.cast<someclass>().firstordefault();
will only ever perform a maximum of one cast from element
to someclass
because linq chains are lazily enumerated.
Source: stackoverflow.com
Related Query
- Is there any performance difference between different linq usage ordering?
- Is there any difference in performance between these two LINQ to SQL queries?
- Are there any performance difference between these 2 linq queries, are they identical or essentially different?
- Why is there a performance difference between LINQ (c#) vs Seq (f#)
- Is there any performance difference between myCollection.Where(...).FirstOrDefault() and myCollection.FirstOrDefault(...)
- Is there any real difference between "and" and "wheres" in linq
- Is there any difference between sql linq
- Performance Difference between LINQ and Stored Procedures
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Is there a performance difference between these two algorithms for shuffling an IEnumerable?
- Is there a difference in performance how a linq expression is constructed?
- Is there a difference between select new T() and select new T in LINQ
- Any performance difference between using variables or constants in Entity Framework query?
- Performance difference between `is` and `as` in LINQ
- Is there a difference between putting conditionals inside or outside a LINQ query?
- Performance difference between Linq Select and ToList
- Is there any significant difference between List.Last() and List.Last<T>()?
- Linq to Entities performance difference between Expression/Func
- LINQ expression to find if there are any matches between two arrays of string
- fetching Id for matching row - any performance difference between two approaches?
- Difference between numbers present in two different lists based on another property using LINQ
- Explain performance differences between different LINQ statements
- What is the difference between LINQ ToDictionary and ToLookup
- Performance of LINQ Any vs FirstOrDefault != null
- foreach + break vs linq FirstOrDefault performance difference
- What is the difference between Contains and Any in LINQ?
- Get the difference between two lists using LINQ
- Is there a difference between Select * and Select [list each col]
- Are there any connections between Haskell and LINQ?
- Is there a way to get the difference between two sets of objects in c#
More Query from same tag
- What is "Linq to SQL"?
- LINQ query is slow
- How to do Many To Many LINQ entity grouping queries?
- Nested Select in LINQ with Lambda Expression
- increase performance of a linq query using contains
- from linq projection into keyvalue pair
- Change condition in LINQ by another condition
- DbContext.SaveChanges() method not committing to SQL Server Database
- how to compare a list type of object with list type int
- LINQ query with a WHERE clause with multiple conditions
- Linq query for joining tables Mvc Asp
- Search List of JTokens for text value
- What is the purpose of LINQ's Expression.Quote method?
- How to save subquery result?
- Add new XElement with data type attribute
- Use dynamic linq or sql query in report generator
- .NET Linq. Confusion about SelectMany
- Creating a Dynamic Where Clause as a Linq Expression
- Group a List based on uniqueness
- Having trouble trying to order using linq
- how to generate where condition without multiple if condition using linq query
- Linq: Get a list of all tables within DataContext
- using where clause after get last record?
- LINQ Group & Sum on a list of Models where a property is another model
- Simple Linq grouping
- Compare two DataRows excluding some columns
- mvc querying an ienumerable property of a class
- Get the count of rows of a DataTable using Linq or without using Linq
- linq where statement subject to a checkbox value
- How do make unwanted namespaces to not appear by default on top of new classes