29

I have following code in my page:

var myVar= Entity.SetName
                 .Where(p => int.Parse(p.ID) >= start &&
                  int.Parse(p.ID) <= end);

start and end are int, but p.ID is string. So i should convert p.ID to int. But i get following error:

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

Where is the problem??

divega
  • 6,320
  • 1
  • 31
  • 31
Seyed Morteza Mousavi
  • 6,855
  • 8
  • 43
  • 69

6 Answers6

24

First, I would highly recommend to check your database design, whether there is a really good reason for ID to be a string. I would consider changing the ID DB type to int and you will get rid of this problem with converting.

The error you get means, that EF does not know how to convert the method Int32.Parse() to SQL. Basically you have two options how to deal with that:

Do the comparison outside the linq to entities:

var myVar= Entity.SetName.AsEnumerable()
                 .Where(p => int.Parse(p.ID) >= start &&
                  int.Parse(p.ID) <= end);

But this is not recommended, because you are reading whole result set from DB, before applying the where condition.

Or make custom model defined function as described in this post on SO:

Convert String to Int in EF 4.0 or Entity Framework: Where do I extend the CSDL/MSL?

Community
  • 1
  • 1
mipe34
  • 5,596
  • 3
  • 26
  • 38
4

First try to convert to int then pass that variable name

int catgry = Convert.ToInt32(customercategory.OWNERSHIP_TYPE);
var customerCateg = (from d in _db.tbl_SIL_CUSTOMER_CATEGORY_MST
    .Where(d => d.CAT_ID == catgry) select d.CAT_TYPE).SingleOrDefault();
  • Same answer as [this](https://stackoverflow.com/a/23520141/861716). Again, this doesn't answer the question how to convert p.ID where p is a range variable. – Gert Arnold Oct 08 '20 at 12:16
0
private void LoadDetail(int id)
    {
        var sp = from category in db.ProductCategories
                 join product in db.Products on category.ProductCategoryID equals product.ProductCategoryID
                 where id == int.Parse(category.ProductCategoryID)
                 select new
                 {
                     product.ProductID,
                     product.ProductName,
                     product.ProductCode,
                     product.Deception,
                     category.CategoryName,
                     product.Quanrity,
                     product.Price
                 };
        DGVDetail.DataSource = sp.ToList();//help: Error: LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
    }

    private void DGVMaster_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        int index = e.RowIndex;
        LoadDetail(index + 1);
    } 
0

To Convert string to int, you have to make it Enumerable, then you can make sort or anything you like

  var list = db.UserEntriesTable.AsEnumerable().Select(x => new {

             Name = x.Name,

             Roll = Convert.ToInt32(x.Roll),

             Mobile = x.Mobile

        }).OrderBy(x => x.Roll).ToList();
Md Shahriar
  • 2,072
  • 22
  • 11
0
int nb = EE.Stagaire.Join(EE.Filiere, S => S.IdFiliere, F => F.IdFiliere, (S, F) => new
        {
            ID = S.Id,
            Name = S.Nom,
            Prénon = S.Prenon,
            Email = S.Email,
            MoteDePass = S.MoteDePass,
            Filiere = F.Filiere1,
        }).Where(S => S.ID.ToString() == r.ToString()).Take(1).Count();
        if (nb != 0)
        {
            MessageBox.Show("find it");

        }

//You can do that if you have to data type is integer

-1

Although it's not efficient, you should be able to load all rows, and then use LINQ to Objects:

var myVar= Entity.SetName.ToList()
                 .Where(p => int.Parse(p.ID) >= start &&
                  int.Parse(p.ID) <= end);
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
  • 5
    Performance!!! with `Entity.SetName.ToList()` If you have 1M entries, then you read them all before u select them that satisfy the `Where` criteria. – Bellash Jul 15 '14 at 15:37
  • he already mentioned "its not efficient". may be for this very specific case he will only have 100s of records. so where is the harm? I dont udnerstand how people down vote answers unless it is not warning and suggesting the "bad" way! – curiousBoy Feb 21 '20 at 16:04