在LINQ中将字符串转换为int到实体? [英] Convert string to int in LINQ to Entities?

查看:365
本文介绍了在LINQ中将字符串转换为int到实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将string值转换为int,但似乎LINQ to Entities不支持此功能.

I have to convert a string value to int, but it seems LINQ to Entities does not support this.

对于以下代码,我遇到了错误.

For the following code, I am getting an error.

var query = (from p in dc.CustomerBranch
             where p.ID == Convert.ToInt32(id) // here is the error.
             select new Location()
             {
                 Name      = p.BranchName,
                 Address   = p.Address,
                 Postcode  = p.Postcode,
                 City      = p.City,
                 Telephone = p.Telephone
             }).First();
return query;

LINQ to Entities无法识别方法'Int32 ToInt32 (System.String)',并且该方法不能转换为商店表达式.

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

推荐答案

在LINQ之外进行转换:

Do the conversion outside LINQ:

var idInt = Convert.ToInt32(id);
var query = (from p in dc.CustomerBranch
             where p.ID == idInt 
             select new Location()
             {
                 Name      = p.BranchName,
                 Address   = p.Address,
                 Postcode  = p.Postcode,
                 City      = p.City,
                 Telephone = p.Telephone
             }).First();
return query;

这篇关于在LINQ中将字符串转换为int到实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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