简体   繁体   English

LINQ to Entities无法识别方法'System.String ToString(Int32)'

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

Hi I am using a linq query which is throwing the error LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression. 您好我正在使用linq查询抛出错误LINQ到实体无法识别方法'System.String ToString(Int32)'方法,并且此方法无法转换为商店表达式。

        List<string> resultMap = (from item in mapResult
                                  select Convert.ToString(item.ResultDE)).ToList();

Error is throwing in this below statement 错误是抛出以下声明

        List<Result_DE> resultList = (from result in db.Result_DE
                                      where result.IsActive == "1"
                                      && resultMap.Contains(Convert.ToString(Convert.ToInt32(result.ID)))
                                      select result).ToList();

please tell me the proper way of writing this query. 请告诉我编写此查询的正确方法。

You cannot use these conversion functions in a LINQ to Entities statement, they cannot be translated to SQL, you need to do the conversions in memory. 您不能在LINQ to Entities语句中使用这些转换函数,它们无法转换为SQL,您需要在内存中进行转换。 But I don't think you need to do that at all. 但我认为你根本不需要这样做。

If you were just using the resultMap to get your resultList , filtered by Results of which the Id is present in mapResult , do the following: 如果您只是使用resultMap来获取您的resultList ,并通过在mapResult存在IdResults进行过滤,请执行以下操作:

var resultList = db.Result_DE
    .Where(r => r.IsActive == "1" && mapResult.Any(mr => mr.ResultDE == r.ID));
    .ToList();

If mapResult is an in-memory collection, instead of an IQueryable that is attached to the db context, you need to do the following: 如果mapResult是内存中集合,而不是附加到db上下文的IQueryable ,则需要执行以下操作:

var resultIds = mapResult.Select(mr => mr.ResultDE).ToList();
var resultList = db.Result_DE
    .Where(r => r.IsActive == "1" && resultIds.Contains(r.ID));
    .ToList();

在调用任何方法(例如ToString())之前,需要使用AsEnumerable()将LINQ转换为Object。

if your item.ResultDE and result.ID is variable type of Int32, why don directly create a List<Int32> ? 如果您item.ResultDEresult.ID是为Int32的变量类型,为什么不直接创建一个List<Int32>

List<Int32> resultMap = (from item in mapResult
                              select item.ResultDE).ToList<Int32>();

List<Result_DE> resultList = (from result in db.Result_DE
                                  where result.IsActive == "1"
                                  && resultMap.Contains(result.ID)
                                  select result).ToList<Result_DE>();

Use SqlFunctions.StringConvert instead of Convert.ToString. 使用SqlFunctions.StringConvert而不是Convert.ToString。

A similar question was asked and answered here 在这里提出并回答类似的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ to Entities无法识别方法&#39;System.String ToString(Int32)&#39;方法 - LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method LINQ to Entities无法识别方法&#39;System.String ToString(Int32)&#39;,并且该方法无法转换为商店表达式。“} - LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression."} LINQ to Entities无法识别方法&#39;System.String ToString(Int32)&#39;方法,并且此方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.String)&#39;方法 - LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method LINQ to Entities无法识别方法'Int32 IndexOf(System.String,System.StringComparison)'方法 - LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String, System.StringComparison)' method LINQ to Entities 无法识别方法 &#39;Int32 Int32(System.String)&#39; 方法,并且此方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression LINQ to Entities无法识别方法&#39;Int32 CompareTo(System.String)&#39;方法 - LINQ to Entities does not recognize the method 'Int32 CompareTo(System.String)' method LINQ to Entities 无法识别“System.String get_Item(Int32)”方法 - LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method LINQ to Entities无法识别方法&#39;Int32 Parse(System.String)&#39;方法, - LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, LINQ to Entities无法识别方法&#39;System.String GetMonthName(Int32)&#39;方法 - LINQ to Entities does not recognize the method 'System.String GetMonthName(Int32)' method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM