score:17
One optimization of your code is to use AsEnumerable()
after the Where
method. If not, every entity is returned from storage, and the entire table is examined using LINQ to Objects. With this simple modification of your code you let the where clause run on sql and retrieve less records from storage. The general rule is to place any query clauses that are implemented by the LINQ provider first.
using (DBContext context = new DBContext())
{
myCollection = context.Items.Where(i => i.Type == 1)
.AsEnumerable().OrderBy(k => k.Name).Select(w => new
{
Alias = w.Name + string.Format("{0}", w.Id),
Name = w.Name
}).ToArray();
}
score:19
EF can't convert String.Format
into SQL, but it handles string concatenations without problem. Use SqlFunctions.StringConvert
instead of String.Format
to convert number into string on server side:
Select(w => new {
Alias = w.Name + SqlFunctions.StringConvert((double)w.Id),
Name = w.Name
})
It generates something like
SELECT
[Extent1].[Name] + STR( CAST( [Extent1].[Id] AS float)) AS [C1],
[Extent1].[Name] AS [Name]
FROM [dbo].[Items] AS [Extent1]
UPDATE: Thus you are using EF provider which does not support this conversion (SQL CE provider cannot cannot translate this query into SQL) you have only one option left - move calculations to client side, as you already have done.
Source: stackoverflow.com
Related Query
- Linq to Entities does not recognize string.Format or concatenation '+'
- LINQ to Entities does not recognize the method 'System.String Format
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- LINQ to Entities does not recognize the method 'Int32 ToInt32 Converting String to Int
- LINQ to Entities does not recognize convert to string
- Error: LINQ to Entities does not recognize the method 'System.String ToString(System.Object)' method occurs while string conversion
- Specific linq exception when converting string to int. LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' m
- LINQ anonym object with result to delimited string (LINQ to Entities does not recognize the method 'System.String ToString()' method)
- 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
- LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)'
- LINQ to Entities does not recognize the method Int32 get_Item(Int32)
- LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method
- LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()'
- Why LINQ to Entities does not recognize the method 'System.String ToString()?
- LINQ to Entities does not recognize the method 'System.Linq.IQueryable`
- LINQ to Entities does not recognize the method 'System.DateTime AddSeconds(Double)' method, and this method cannot be translated into
- LINQ to Entities does not recognize the method 'System.Object GetValue(...)'
- 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 ElementAt(i);
- LINQ to Entities does not recognize the method 'System.String StringConvert(System.Nullable`1[System.Double])
- 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 IndexOf(System.String, System.StringComparison)' method
- LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String get_Item(System.String)' method
- LINQ to Entities does not recognize the method 'System.String[] Split(Char[])' method,
- LINQ to Entities does not recognize the method Generic.List(int) to Generic.IEnumerable(int) method
- LINQ to Entities does not recognize the method exception
- LINQ to Entities does not recognize the method 'Method name' method
- LINQ to Entities does not recognize the method 'Int32
More Query from same tag
- When returning the count from SQL, how to also include the value in grouped item for which the count doesn't exist?
- Linq query where in list, performance what is the best?
- Using Distinct with Linq to SQL
- How to Linq to yml
- Oracle Entity Framework - Call custom function (EDIT_DISTANCE)
- How to combine 2 linq statments with groupby clause into 1
- Different results between yield return and LINQ Select
- Page-by output. LINQ, Skip() and Take()
- how to build html navigation bar dynamically using data from database
- When I use an IQueryable function, it significantly increase the time of the query
- Simple XML parsing with LINQ
- How can I match substrings of a list's elements in another list?
- LINQ join using dates not working
- Efficiently Filtering One DataTable matching multiple columns of another DataTable in C#
- Performing 1-to-1 Left Outer Join in LINQ using Lambda expression
- Dynamically-built LINQ query gives inconsistent results depending on how it's built
- LINQ Select n list ASP.NET Core
- Should a reference to a null variable throw exception
- How to copy a row range from one column to others in a column oriented c# datatable?
- Group by and sum query on datatable
- Automapper unable to project one enum type to another
- Basic array Any() vs Length
- Can I change this Query into a method?
- send result of ToList() from hub to client
- How I can pass the LINQ result anonymous type to another method?
- LINQ: How to remove element from IQueryable<T>
- Count instances of element in c# list
- LINQ is able to filter on association, but returns 0 results when referenced directly
- How to sort a list<datarow>?
- How to convert a ISingleResult<T> containing a string to a string array