C# LINQ to Entities 在尝试解析不等式比较的列时无法识别方法“Int32 Parse(System.String)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14558837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:15:16  来源:igfitidea点击:

LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method when attempting to parse a column for inequality comparisons

c#entity-framework

提问by Seyed Morteza Mousavi

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.IDis string. So i should convert p.IDto int. But i get following error:

start 和 end 是 int,但p.ID是 string。所以我应该将p.ID转换为 int。但我收到以下错误:

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 无法识别方法 'Int32 Parse(System.String)' 方法,并且此方法无法转换为存储表达式。

Where is the problem??

问题出在哪儿??

采纳答案by mipe34

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

首先,我强烈建议您检查您的数据库设计,是否有充分的理由ID成为string. 我会考虑将ID数据库类型更改为int,您将通过转换.

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:

您得到的错误意味着 EF 不知道如何将方法转换Int32.Parse()为 SQL。基本上你有两种选择如何处理:

Do the comparison outside the linq to entities:

在 linq 之外对实体进行比较:

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 wherecondition.

但是不推荐这样做,因为在应用where条件之前,您正在从数据库读取整个结果集。

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

或者按照SO 上的这篇文章中的描述创建自定义模型定义的函数

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

在 EF 4.0实体框架中将字符串转换为 Int :我在哪里扩展 CSDL/MSL?

回答by Aaron Anodide

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

虽然效率不高,但您应该能够加载所有行,然后使用 LINQ to Objects:

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

回答by Zubaer Naseem

move the parse function out of linq expression.

将解析函数移出 linq 表达式。

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

回答by user4536768

First try to convert to intthen pass that variable name

首先尝试转换为int然后传递该变量名称

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

回答by Quang Hoà

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