LINQ to Entities无法识别方法'System.String ToString(Int32)' [英] LINQ to Entities does not recognize the method 'System.String ToString(Int32)'
问题描述
我正在使用linq查询,该查询将LINQ抛出错误到实体,但无法识别方法'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.
List<string> resultMap = (from item in mapResult
select Convert.ToString(item.ResultDE)).ToList();
下面的语句中引发错误
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.
推荐答案
您不能在LINQ to Entities语句中使用这些转换函数,它们无法转换为SQL,您需要在内存中进行转换.但我认为您根本不需要这样做.
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. But I don't think you need to do that at all.
如果您只是使用resultMap
来获取resultList
,并按Results
进行过滤,而Id
存在于mapResult
中,请执行以下操作:
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:
var resultList = db.Result_DE
.Where(r => r.IsActive == "1" && mapResult.Any(mr => mr.ResultDE == r.ID));
.ToList();
如果mapResult
是内存中的集合,而不是附加到db
上下文的IQueryable
,则需要执行以下操作:
If mapResult
is an in-memory collection, instead of an IQueryable
that is attached to the db
context, you need to do the following:
var resultIds = mapResult.Select(mr => mr.ResultDE).ToList();
var resultList = db.Result_DE
.Where(r => r.IsActive == "1" && resultIds.Contains(r.ID));
.ToList();
这篇关于LINQ to Entities无法识别方法'System.String ToString(Int32)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!