0
votes

Here is the query

db.setupBrands.Where(x => 
    Convert.ToInt32(x.SortKey) <= id && 
    Convert.ToInt32(x.SortKey) >= desID)
.Select(x => x);

Here SortKey is string type i want to convert to int. On Convert.ToInt32() i got the following error.

LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.

1
Out of interest, if your SortKey property is always an integer, why do you have it as a string in your schema?Jon Skeet
SortKey is String Type... @Jon SkeetZeb-ur-Rehman
Yes, I can tell that. My point is why? If it's always got a numeric value, why don't you make it an integer in your schema?Jon Skeet
Because i want to show it in 0001 form if i keep it int i will store it as 1.Zeb-ur-Rehman
@Zeb-ur-Rehman you can format integer value for displaying String.Format("{0:0000}", value)Sergey Berezovskiy

1 Answers

2
votes

EF can't translate Convert and Parse. I completely agree with the above, but, if your SortKey is nchar(4) you can try this:

string s_id = string.Format("{0:0000}", id);
string s_desID = string.Format("{0:0000}", desID );

db.setupBrands.Where(x => 
    x.SortKey <= s_id && 
    x.SortKey >= s_desID)
.Select(x => x);