LINQ to Entities无法识别方法'Int32 ToInt32(System.String)'方法 [英] LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method

查看:153
本文介绍了LINQ to Entities无法识别方法'Int32 ToInt32(System.String)'方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在线检查了一些答案,并且我知道我无法将ToInt32转换为T-SQL,因此我需要先在内存中运行查询,然后再进行转换,但是我不知道如何应用在我的示例中.我有下面的查询,它向我显示标题中写的错误消息:

I checked online for some answers, and I understood that I cannot translate ToInt32 to T-SQL, and I need to use run my query in-memory first and then do the conversion, but I don't know how to apply it in my example. I have the query below, and it shows me the error message written in the title:

string[] parts = rvm.ZipCode.Split('-');
var list = results.OrderBy(b => 
  Math.Abs(Convert.ToInt32(parts[0]) - Convert.ToInt32(b.Zip))).Take(5).ToList();

其中results是我之前应用的另一个查询.知道如何解决吗?

Where results is another query that I applied earlier. Any idea how to solve it?

推荐答案

您可以尝试以下方法:

// It would be better, if we make the conversion only once instead of 
// doing this each time we fetch an item from our collection.
int value = Convert.ToInt32(parts[0]);

var list = results.AsEnumerable()
                  .OrderBy(z => Math.Abs(value - Convert.ToInt32(z.Zip)))
                  .Take(5)
                  .ToList();

这篇关于LINQ to Entities无法识别方法'Int32 ToInt32(System.String)'方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆