Zeb-ur-Rehman
Zeb-ur-Rehman

Reputation: 1209

Convert string to int in linq query

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.

Upvotes: 0

Views: 1199

Answers (1)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

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);

Upvotes: 2

Related Questions