score:0
Does this still need answering?
As you already found out, F# anonymous types order fields by name (not by source code order of declaration, as C# anonymous types would do).
When writing {| B = c.CategoryID; A = c.CategoryName; |}
in a LINQ query, this will not actually pass an anonymous type, rather the compiler creates an System.Linq.Expressions.Expression
that describes how to construct the anonymous type, and later on the underlying framework implementing LINQ (e.g., the Entity Framework) will try to parse that expression (and create e.g. SQL code from it).
Problem here is, c.CategoryID
and c.CategoryName
may have side effects, hence the compiler will evaluate them in the order specified in the source code (first ID, then Name), but assign them in the order of the anonymous type.
Long story short, the generated System.Linq.Expressions.Expression
first will evaluate c.CategoryID
, assign the value to a temporary variable, evaluate c.CategoryName
, assign that to the anonymous type's first field, and finally assign the temporary variable to the anonymous type's second field. And the EF translator later on does not know how to handle the temporary variable (e.g., how to translate that to SQL.)
(In C#, no field reordering happens, so no temporary variables are required to mask side effects, so the expression parser will not face that problem.) (The F# anonymous type at present is not fully fit for LINQ.)
Source: stackoverflow.com
Related Query
- Why could the F# LINQ expression not be translated - at least partially?
- The LINQ expression could not be translated and will be evaluated locally
- The LINQ expression could not be translated
- The LINQ expression could not be translated. Eiither rewrite the query in a form that can be translated
- The LINQ expression could not be translated - Entity Framework Core
- The LINQ expression could not be translated about sum of Property TimeSpan
- EF Core: The LINQ expression could not be translated
- System.InvalidOperationException: 'The LINQ expression could not be translated. Either rewrite the query in a form that can be translated
- The LINQ expression 'Expression' could not be translated. Either rewrite the query in a form that can be translated
- InvalidOperationException: The LINQ expression could not be translated
- The LINQ expression could not be translated although ToLists is used
- The LINQ expression 'GroupByShaperExpression: could not be translated while using GROUPBY in LINQ query
- EF Core: The LINQ expression could not be translated - Net Core 3.1
- Get next element from Query after element with specified Id. The LINQ expression could not be translated
- EF Core 3.1 - The LINQ expression could not be translated (left joins with group by)
- EF Core: The LINQ expression could not be translated for a nested List with Generic StartsWith() Expression
- The LINQ expression could not be translated while trying to sort
- The LINQ expression could not be translated - Error with Nullable Id field
- The LINQ expression could not be translated using Shadow Properties in MVC Core
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- 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
- The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation EF Core 3.1
- The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation
- Why "The LINQ expression 'x' could not be translated" ? I'm not using "Where()"
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression
- 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 Entities does not recognize the method and this method cannot be translated into a store expression
More Query from same tag
- LINQ Syntax for querying a dictionary.ToList().Where()
- Search, Filter AND Remove a List<List<T>>
- Discards inside C# Linq queries
- C# Linq - EF, select grouped data having the max value
- Preserve order of values with linq and ToLookup()
- Can't make GroupJoin work. NavigationExpandingExpressionVisitor exception
- How to rollback changes to WPF DataGrid control using LINQ-to-SQL?
- Optimizing query with many-to-many relationship on big data set
- C# LINQ for tree oriented data structure of classes
- Linq query makes all calls to bring foreign key's table data
- Use linq OrderBy to order a list by an ID
- How to select an attribute in XML based on the value of another attribute?
- XPathSelectElements always returns Expression must evaluate to a node-set
- How can I flatten a collection of objects (which in turn contain collections)?
- Convert String to DateTime in LINQ query with Entity Framework Core
- Multi-dimensional arrays in Linq
- How to create datagrid columns from multiple records using LinqtoSql?
- Change SQL Query to LINQ, asp.net MVC
- Grabbing a specific value from a List of Entitiy
- DATETIME Error(Finding total days)
- How to query a DataSet and set the result as a DataSource for some control? (C# winforms)
- Fetch Grouped data in ViewModel
- Dictionary<string, JToken> recursive search
- Reducing Memory Footprint
- How can I use linq to initialize an array of repeated elements?
- Return the result of joining two tables in Silverlight 4.0
- LINQ SQL Connection String Not Working When Read From TXT File
- Conditional filter on list
- LINQ to SQL: Order 2 Columns into a new Column
- LINQ to SQL how optimise query?