LINQ - '无法翻译表达式'与以前使用和验证的查询条件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LINQ - '无法翻译表达式'与以前使用和验证的查询条件相关的知识,希望对你有一定的参考价值。
我是LINQ的新手,无法解决一些行为上的不一致问题。任何知识渊博的输入将非常感激。我在SO和其他地方看到类似的问题,但它们似乎没有帮助。
我有一个非常简单的设置 - 公司表和地址表。每个公司可以有0个或更多地址,如果> 0,则必须指定一个地址。我正在尝试处理有0个地址的情况,使用外连接并相应地更改select语句。
请注意我目前正在将输出绑定到GridView,所以我想在查询中保留所有处理。
以下内容有效
IQueryable query =
from comp in context.Companies
join addr in context.Addresses on comp.CompanyID equals addr.CompanyID into outer // outer join companies to addresses table to include companies with no address
from addr in outer.DefaultIfEmpty()
where (addr.IsMain == null ? true : addr.IsMain) == true // if a company has no address ensure it is not ruled out by the IsMain condition - default to true if null
select new {
comp.CompanyID,
comp.Name,
AddressID = (addr.AddressID == null ? -1 : addr.AddressID), // use -1 to represent a company that has no addresses
MainAddress = String.Format("{0}, {1}, {2} {3} ({4})", addr.Address1, addr.City, addr.Region, addr.PostalCode, addr.Country)
};
但这会在GridView中显示一个空地址为“, , ()
”
所以我更新了MainAddress字段
MainAddress = (addr.AddressID == null ? "" : String.Format("{0}, {1}, {2} {3} ({4})", addr.Address1, addr.City, addr.Region, addr.PostalCode, addr.Country))
现在我得到了Could not translate expression
错误和错误中的一堆spewey自动生成的代码,这对我来说意义不大。
我添加到MainAddress的条件与AddressID上的工作条件没有什么不同,所以有人能告诉我这里发生了什么吗?
任何帮助是极大的赞赏。
您得到的错误告诉您LinqToSql无法将您的空检查转换为String.Format表达式转换为SQL。如果你查看第一个查询生成的SQL(使用LinqPad或SQL Profiler),你会看到类似的东西:
SELECT [t0].[CompanyID], [t0].[Name],
(CASE
WHEN [t1].[AddressID] IS NULL THEN @p0
ELSE [t1].[AddressID]
END) AS [AddressID],
[t1].[Address1] AS [value],
[t1].[City] AS [value2],
[t1].[Region] AS [value3],
[t1].[PostalCode] AS [value4],
[t1].[Country] AS [value5]
FROM [Company] AS [t0]
LEFT OUTER JOIN [Address] AS [t1] ON [t0].[CompanyID] = [t1].[CompanyID]
WHERE ([t1].[IsMain] IS NULL) OR ([t1].[IsMain] = 1)
对于你的AddressID
字段,你可以看到当CASE-WHEN
为null时它使用AddressID
来处理条件。当您为CASE-WHEN
添加MainAddress
时,它正在尝试为该字段执行相同的操作,但是没有SQL可以用于string.Format
它可以用于ELSE
子句,因此它会爆炸。
解决此问题的一种简单方法是使用方法格式化字符串。通过调用私有方法,LinqToSql不会尝试将string.Format
转换为SQL,而是返回填充Address
对象所需的所有字段。然后该方法可以处理格式化。
例如:
LINQ:
....
select new {
comp.CompanyID,
comp.Name,
AddressID = (addr.AddressID == null ? -1 : addr.AddressID),
MainAddress = FormatAddress(addr)
};
方法:
private static string FormatAddress(Address addr)
{
return (addr == null ? "" :
string.Format("{0}, {1}, {2} {3} ({4})",
addr.Address1, addr.City,
addr.Region, addr.PostalCode, addr.Country));
}
以上是关于LINQ - '无法翻译表达式'与以前使用和验证的查询条件的主要内容,如果未能解决你的问题,请参考以下文章