在 Linq 中使用函数
我有这个查询:
var accounts =
from account in context.Accounts
from owner in account.AccountOwners
join business in context.Businesses
on account.CreditRegistryId
equals business.RegistryId
join astatus in context.AccountStatuses
on account.AccountStatusId
equals astatus.AccountStatusId
join LS in context.LegalStatuses
on account.LegalStatusId
equals LS.LegalStatusId
where !excludeTypes.Contains(account.AccountType)
select new AccountsReport
{
AccountTypeDescription = GetAccountTypeDescription(account.AccountType),
AccountNumber = 1,
AccountStatus = "aasd",
CreditorAddress = "address",
CreditorCity = "my city",
CreditorName = "creditor name",
CreditorState = "my state",
LegalStatus = "my status",
RegistryId = 121323
};
给出错误:
LINQ to Entities does not recognize the method 'System.String GetAccountTypeDescription(System.String)' method, and this method cannot be translated into a store expression.
功能是:
public string GetAccountTypeDescription(string accountType)
{
var result = context.AccountTypes.Where(x => x.AccountTypeCode == accountType).Select(x => x.Abbreviation).SingleOrDefault();
if (string.IsNullOrEmpty(result))
{
result = accountType;
}
return result;
}
如果我不在 LINQ 查询中使用 GetAccountTypeDescription,它会起作用。
请建议解决方案
I have this query:
var accounts =
from account in context.Accounts
from owner in account.AccountOwners
join business in context.Businesses
on account.CreditRegistryId
equals business.RegistryId
join astatus in context.AccountStatuses
on account.AccountStatusId
equals astatus.AccountStatusId
join LS in context.LegalStatuses
on account.LegalStatusId
equals LS.LegalStatusId
where !excludeTypes.Contains(account.AccountType)
select new AccountsReport
{
AccountTypeDescription = GetAccountTypeDescription(account.AccountType),
AccountNumber = 1,
AccountStatus = "aasd",
CreditorAddress = "address",
CreditorCity = "my city",
CreditorName = "creditor name",
CreditorState = "my state",
LegalStatus = "my status",
RegistryId = 121323
};
which is giving error:
LINQ to Entities does not recognize the method 'System.String GetAccountTypeDescription(System.String)' method, and this method cannot be translated into a store expression.
Function is :
public string GetAccountTypeDescription(string accountType)
{
var result = context.AccountTypes.Where(x => x.AccountTypeCode == accountType).Select(x => x.Abbreviation).SingleOrDefault();
if (string.IsNullOrEmpty(result))
{
result = accountType;
}
return result;
}
If I don't use GetAccountTypeDescription in LINQ query, It works.
Please suggest solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 LINQ 查询不是在本地运行,而是转换为表达式(又转换为 SQL)并在您的数据库服务器上执行。表达式解析器无法将您的函数转换为 SQL,因为该函数在服务器上不存在。您只能在要在数据库上运行的 LINQ 查询中使用 LINQ 和其他一些 .NET 函数。你可以很容易地解决这个问题
You LINQ query is not run locally, but is turned into an expression (which is turned into SQL) and executed your database server. The expression parser cannot turn your function into SQL because that function doesn't exist on the server. You can only use LINQ and a few other .NET functions inside a LINQ query that is to be run on the database. You can easily fix this as such
编辑 - 添加左连接
Edit - Adding a left join
我有点不明白这个。为什么不为类/结构 AccountReport 提供一个属性,该属性为您提供 AccountTypeDescription...这样您就封装了逻辑,隐藏了实现细节,并且通常使代码更加干净。由于帐户描述只是对提取的数据进行转换,因此这是最好的方法。
卢克
I kind of don't understand this. Why don't You provide a property to the class/struct AccountReport that gives YOu AccountTypeDescription... That way You encapsulate the logic, hid the implementation details and generally make the code a lot kosher. Since the Accoutn description is just a transformation on the pulled data that is the best way to do.
luke
您不能在 linq-to-entities 中使用任意函数。已经解释过很多次了。例如此处。如果要在 Linq-to-entities 查询中使用自定义函数,则必须将其定义为数据库中的 SQL 用户定义函数 (UDF) 并映射它。
首先创建函数
现在您必须更新模型并导入函数(它将在存储过程中列出)。将函数导入 SSDL(EDMX 中的存储描述)后,您可以映射函数:
此函数只是表达式树中使用的占位符。当存储提供者将表达式树转换为 SQL 查询时,它将被映射的 SQL 函数替换。现在您可以在 Linq-to-entities 查询中使用它:
You cannot use arbitrary function in linq-to-entities. It was explained many times. For example here. If you want to use custom function in Linq-to-entities query you must define it as SQL User defined function (UDF) in your database and map it.
First create function
Now you must update your model and import the function (it will be listed among stored procedures). Once you have your function imported in SSDL (storage description in EDMX) you can map the function:
This function is only placeholder used in expression tree. It will be replaced by mapped SQL function when store provider translates expression tree to SQL query. Now you can use this in your Linq-to-entities query: