gpt4 book ai didi

entity-framework - 在LINQ中将字符串转换为int到实体?

转载 作者:行者123 更新时间:2023-12-04 07:26:57 27 4
gpt4 key购买 nike

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

对于以下代码,出现错误。

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 does not recognize the method 'Int32 ToInt32 (System.String)', and this method can not be translated into a store expression.

最佳答案

在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;

关于entity-framework - 在LINQ中将字符串转换为int到实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7399602/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com