C# LINQ to Entities 无法识别方法 'Int32 ToInt32(System.Object)' 方法,并且此方法无法转换为存储表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10117681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
提问by Only Bolivian Here
Here's what I'm trying to do:
这是我想要做的:
public List<int> GetRolesForAccountByEmail(string email)
{
var account = db.Accounts.SingleOrDefault(a => a.Email == email);
if (account == null) return new List<int>();
return db.AccountRoles.Where(a => a.AccountId == account.AccountId).Select(a => Convert.ToInt32(a.RoleId)).ToList();
}
I had to convert to Int32 because I could not return a List<int?>when the method was to return a List<int>.
我不得不转换为 Int32,因为List<int?>当方法返回 a时我无法返回 a List<int>。
Any suggestions on how to solve this simple problem?
关于如何解决这个简单问题的任何建议?
采纳答案by Andrew Barber
Instead of this:
取而代之的是:
Select(a => Convert.ToInt32(a.RoleId))
Do this:
做这个:
Select(a => a.RoleId.Value)
The reason is in the error description; when you are doing these queries through IQueryable, the methods being used within the selector have to be something that can be translated into a SQL query or function. In this case Convert.ToInt32()is not such a method. For intfields with nullallowed, using the .NET .Valueproperty does work, though.
原因在错误描述中;当您通过 IQueryable 执行这些查询时,选择器中使用的方法必须可以转换为 SQL 查询或函数。在这种情况下Convert.ToInt32()不是这样的方法。不过,对于允许的int字段null,使用 .NET.Value属性确实有效。
Note that this would not work if your RoldIdis null, however. You'll get an InvalidOperationException. You might want to return a set value instead if the backing field is null:
请注意,如果您RoldId是null,这将不起作用。你会得到一个InvalidOperationException. 如果支持字段为空,您可能希望返回一个设置值:
Select(a => a.RoleId.HasValue ? a.RoleId.Value : int.MinValue)
This will return the value if there is one, and int.MinValueif not.
如果有,这将返回值,int.MinValue如果没有。
回答by muthuvel
Use this: Select(a => (int)a.RoleId)
使用这个: Select(a => (int)a.RoleId)
回答by Mohamed Alikhan
Try to take List object from the db.Accounts and do the stuff. It works for me.
尝试从 db.Accounts 中获取 List 对象并执行这些操作。这个对我有用。
public List<int> GetRolesForAccountByEmail(string email)
{
var account = db.Accounts.SingleOrDefault(a => a.Email == email);
if (account == null) return new List<int>();
return db.AccountRoles.Where(a => a.AccountId == account.AccountId).Select(a => Convert.ToInt32(a.RoleId)).ToList();
}
Instead of this, try this..
而不是这个,试试这个..
public List<int> GetRolesForAccountByEmail(string email)
{
var account = db.Accounts.SingleOrDefault(a => a.Email == email);
if (account == null) return new List<int>();
List<AccountRoles> accountRoles= db.AccountRoles.ToList<AccountRoles>();
return accountRoles.Where(a => a.AccountId == account.AccountId).Select(a => Convert.ToInt32(a.RoleId)).ToList();
}

