score:3
in your second example, the call to select is not redundant. if you would omit the select call, the query would just return the original collection, whereas select returns an ienumerable.
in your first example, where already returns an ienumerable and the select clause doesn't do any work, so it is omitted.
score:1
because in the query version there is no actual select projecting 'c' into something else, it is just passing on 'c' as-is. which results in only a call to 'where'.
in the second variation, you explicitly call 'select' and thus do a projection. yes, you are only returning the same objects, but the compiler will not see this.
score:5
the c# compiler is clever and remove useless statement from linq. select c is useless so the compiler remove it. when you write select(c=>c) the compiler can't say that's the instruction is useless because it' a function call and so it doesn't remove it. if you remove it yourself il become the same.
edit : linq is a "descriptive" language : you say what you want and the compiler transforms it well. you don't have any control on that transformation. the compiler try to optimize function call and don't use select because you don't do projection so it's useless. when you write select(c => c) you call a function explicitely so the compiler won't remove it.
var a = from c in companies select c;
var a = c.select(elt=>elt);
select is usefull in this example. if you remove it a has the type of c; otherwise a is an ienumerable
score:5
@mexianto is of course correct that this is a compiler optimization.
note that this is explicitly called out in the language specification under "degenerate query expressions." also note that the compiler is smart enough to not perform the optimization when doing so would return the original source object (the user might want to use a degenerate query to make it difficult for the client to mutate the source object, assuming that it is mutable).
7.16.2.3 degenerate query expressions
a query expression of the form
from x in e select x
is translated into
( e ) . select ( x => x )
[...] a degenerate query expression is one that trivially selects the elements of the source. a later phase of the translation removes degenerate queries introduced by other translation steps by replacing them with their source. it is important however to ensure that the result of a query expression is never the source object itself, as that would reveal the type and identity of the source to the client of the query. therefore this step protects degenerate queries written directly in source code by explicitly calling select on the source. it is then up to the implementers of select and other query operators to ensure that these methods never return the source object itself.
Source: stackoverflow.com
Related Query
- Differences in LINQ vs Method expression
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- How to retrieve last 5 records using LINQ method or query expression in C#
- LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- LINQ WHERE method alters source collection
- LINQ to Entities does not recognize the method 'Boolean HasFlag(System.Enum)' when creating the expression via System.Linq.Expressions.Expression
- How to reuse a linq expression for 'Where' when using multiple source tables
- extracting method name from linq expression
- Convert Method to Linq Expression for query
- LINQ Source Code Available
- Build expression tree for LINQ using List<T>.Contains method
- Call Ignore Case for Contains Method using a generic LINQ Expression
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression
- How merge expression in the Select method with Linq
- Convert linq query expression with multiple froms into extension method syntax
- Linq Evaluating a method as a lambda expression
- Workaround to use a method within a LINQ expression
- Linq expression to return the first overloaded method that takes 1 string parameter
- LINQ Is it possible to get a method name without a return type via LINQ expression trees?
- LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression
- creating Linq to sqlite dbml from DbLinq source code
- LINQ to Entities does not recognize the method 'Double ToDouble(System.String)' method, and this method cannot be translated into a store expression
- Linq to Sql: Optimizing lamba expression - clean code
- LINQ to Entities does not recognize the method and this method cannot be translated into a store expression
- Construct a Linq Expression from a generic method
- C# - Code supposed to be unreachable when calculate value in LINQ sum method
More Query from same tag
- Linq and EntityFramework 4 with multiple inner joins with nested subqueries
- add two entry for every row - Linq C#
- How can I stop intellisense from auto completing 'from' when typing linq query
- Select top 1 result from subquery in linq to sql
- Indexed access to XElement‘s child nodes
- LINQ to create list of strings from indexes stored in int[]
- Error: Cannot redefine the namespace for prefix '' used at current element
- Converting JSON array to JSON Object
- Retrieve a list of specific elements from an XDocument using LINQ
- Compare two lists that contain a lot of objects (3th part) "those objects have different type"
- LINQ orderby in subquery
- Reducing code complexity with LINQ
- Optimized way to select items from a Collection excluding a list of certain member using Linq
- split field a string in a linq
- How can I concat List<type>?
- Combine Expression (Expression<Func<TIn,TOut>> with Expression<Func<TOut, bool>>)
- How to Concat Classes with Inheritance and same Table in Database with Linq and EntityFramework 6?
- Batch Update using LINQ
- How to check the attribute value string from linq in c#
- LINQ to Objects filtering
- List of object - combining condition check within the list with local variables outside of it
- EF Core 2.2: Add timezone conversion to a datetime2 column on select, groupby and/or where (Modify/enrich property mapping on a query)
- Linq to Entities simple group query
- XML & Lambda - accessing fields that don't implement the IEnumerable interface
- Cache LINQ query - IEnumerable.Skip()
- linq to sql query with multiple where parameters
- How to convert a query with inner join in sql to linq
- Is it possible to combine these 2 linq queries?
- c# Connecting to database using MySQL
- C# orderby string along with int