Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want like operator for interger field,then i am using this code

var details = db.RecipientDescriptions.Where(o =>o.ID.ToString().Contains("67")).ToList();

but i am getting the error

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

any solutions please?
Posted
Updated 19-Jun-20 20:22pm
Comments
[no name] 12-Aug-13 7:13am    
"any solutions please", sure.... don't call ToString on a string. It's a string already so there is no need to convert it to a string.
Usha Sanjee 12-Aug-13 7:34am    
ID is an integer field

As the expression past to a LINQ to Entities query is run on the DB you've restricted in what you can pass. Since ToString is just a method that can do anything there's no way for the DB to guarantee the result.

What you can do if your unfiltered result set isn't too large is to apply the Where clause after the expression has been run;
C#
db.RecipientDescriptions.ToList().Where(o =>o.ID.ToString().Contains("67").ToList();


On a more general note I would say that if you have a need to do contains on a value then that value shouldn't be of type integer.

Hope this helps,
Fredrik
 
Share this answer
 
Copied from:

LINQ to Entities does not recognize the method 'System.String ToString()' method[^]

use can use something like this,

C#
where userIds.Contains(SqlFunctions.StringConvert((double)user.Id))
instead of where userIds.Contains(user.Id.ToString())
 
Share this answer
 
Hi,

Actually you are using LINQ to Sql so those objects are IQueryable so there is no ToString() method.

you can try this :

var details = db.RecipientDescriptions.Where(o =>SqlMethods.Like(o.ID, "67%")).ToList();
 
Share this answer
 
You can use Contains in Linq for Like.
If it's an integer value you need to use
AsEnumerable()
and
"~~"

string strInvoiceNum = "~~800~~8999~~9000-~~9010~~";

var vdbInvoiceExist = (from row in Db.InvcHead.AsEnumerable()
                        where row.Company == callContextClient.CurrentCompany
                        && strInvoiceNum.Contains("~~" + row.InvoiceNum + "~~")
                        select row);


If it's an string

string strCustID = "~~Addis~~BARRISTON~~CLARKEo~~ECD-~~";
vdbCustomerExist = (from row in Db.Customer
            where row.Company == callContextClient.CurrentCompany && strCustID.Contains("~~" + row.CustID + "~~")
            select row);
 
Share this answer
 
Comments
Maciej Los 22-Jun-20 5:07am    
Wrong! Please, read solution#3.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900