自从我使用Entity Framework以来已经有一段时间了,我又回到了EF 5中,但这个问题不会:
SELECT
c.OrganizationName as CompanyName,
c.OrganizationKey as CompanyId,
ISNULL(a.Line1, '') as Line1,
ISNULL(a.Line2, '') as Line2,
a._CityStateZip as CityStateZip
FROM Organizations c
JOIN Addresses a ON c.AddressKey = a.AddressKey
WHERE c.OrganizationName LIKE @term + '%'
AND c.IsSuspended = 0
AND c.IsActive = 1等同于:
var results = (from c in adms.Organizations
where c.OrganizationName.StartsWith(term)
where !c.IsSuspended
where c.IsActive
select new
{
CompanyName = c.OrganizationName,
CompanyId = c.OrganizationKey,
Line1 = (string.IsNullOrEmpty(c.Address.Line1) ? string.Empty : c.Address.Line1),
Line2 = (string.IsNullOrEmpty(c.Address.Line2) ? string.Empty : c.Address.Line2),
CityStateZip = c.Address._CityStateZip
}).ToList();当我运行LINQ to SQL代码时,我得到以下错误:
Could not translate expression
'Table(Organization).Where(c => c.OrganizationName
.StartsWith(Invoke(value(System.Func`1[System.String]))))
.Where(c => Not(c.IsSuspended))
.Where(c => c.IsActive)
.Select(c => new <>f__AnonymousType2`5(
CompanyName = c.OrganizationName,
CompanyId = c.OrganizationKey,
Line1 = IIF(IsNullOrEmpty(c.Address.Line1),
Invoke(value(System.Func`1[System.String])), c.Address.Line1),
Line2 = IIF(IsNullOrEmpty(c.Address.Line2),
Invoke(value(System.Func`1[System.String])), c.Address.Line2),
CityStateZip = c.Address._CityStateZip))'
into SQL and could not treat it as a local expression.我是不是完全漏掉了什么?我想我可以在LINQ to SQL中使用string.IsNullOrEmpty。
发布于 2012-12-20 03:18:50
用""替换string.Empty。遗憾的是,EF不支持string.Empty。
一般来说,EF LINQ的支持非常糟糕。要时刻意识到这个问题。这是EF悲痛的常见原因。
LINQ to SQL在通用语言习惯用法方面没有问题。
顺便说一句,你可以更好地重写这个::c.Address.Line1 ?? ""。
发布于 2012-12-20 03:12:18
(string.IsNullOrEmpty(c.Address.Line1) ? string.Empty : c.Address.Line1)正被翻译成
IIF(IsNullOrEmpty(c.Address.Line1), Invoke(value(System.Func`1[System.String])), c.Address.Line1)您所要做的就是,如果字符串值已经为空或"",则将其设置为""。
您应该尝试只使用Line1 = c.Address.Line1
https://stackoverflow.com/questions/13959347
复制相似问题