IEnumerable<Element>.Cast<SomeClass>().FirstOrDefault(); is probably want you want, but the answer has nothing to do with performance.
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
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
IEnumerable<Element>.Cast<SomeClass>().FirstOrDefault(); will only ever perform a maximum of one cast from
SomeClass because LINQ chains are lazily enumerated.
- Is there any performance difference between different linq usage ordering?
- Why is there a performance difference between LINQ (c#) vs Seq (f#)
- 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?
- Performance Difference between LINQ and Stored Procedures
- Is there any performance difference between myCollection.Where(...).FirstOrDefault() and myCollection.FirstOrDefault(...)
- 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
- Is there any real difference between "and" and "wheres" in linq
- Performance difference between `is` and `as` in LINQ
- Is there a difference between putting conditionals inside or outside a LINQ query?
- Is there any difference between sql linq
- Performance difference between Linq Select and ToList
- Linq to Entities performance difference between Expression/Func
- 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
- foreach + break vs linq FirstOrDefault performance difference
- Get the difference between two lists using LINQ
- Is there a difference between Select * and Select [list each col]
- Is there a way to get the difference between two sets of objects in c#
- Linq way to get piecewise difference between element and next element in list
- Difference between Linq to Sql, Linq, Typed Datasets, ADO.NET
- What is the difference between LINQ query expressions and extension methods
- Difference between Sum and Aggregate in LINQ
- Difference between cast and as inside a select in LINQ
- What is the difference between Joining two different DB Context using ToList() and .AsQueryable()?
- Logical difference between LINQ Where() and FirstOrDefault()
- Is there a good source that gives an overview of linq optimizations?
- Unable to cast a object of type 'SZArrayEnumerator' to type 'System.Collections.Generic.IEnumerator' 1[System.String]'
- Linq method equivalent for "Into"
- How to convert a linq query to ToList() to use extended properties
- SQL Like clause in LINQ
- linq: how to check if the value of one attribute exists
- LINQ Where clause throws casting exception from string to Guid even though comparison only contains Guids
- Many-to-many mapping entities in LINQ
- linqpad error using Rx Reactive extensions in c#
- Cannot implicit convert type List to IEnumerable while grouping with Linq
- LINQ finding the highest frequency
- sql equivalent linq expression
- Debugging LINQ on Windows Phone
- Confused on Getting an int out of a var from Linq2Sql
- LINQ return a value IEnumerable
- linq to xml with C# and VS2008
- Unable to Insert Char(63) by SQL query
- How do I search through a property for a particular value?
- Group objects into fixed-sized collections
- Assign an object to the class from within
- Unable to convert SQL Query to LINQ Query for Left Outer Join