简体   繁体   English

string.IsNullOrEmpty + Entity Framework 5

[英]string.IsNullOrEmpty + Entity Framework 5

It's been a while since I've used Entity Framework, and I'm jumping back in with EF 5, but wouldn't this query: 我已经使用了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

be the same as: 与以下相同:

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();

When I run the LINQ to SQL code, I get the following error: 当我运行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.

Am I completely missing something here? 我在这里完全遗漏了什么吗? I thought I could use string.IsNullOrEmpty with LINQ to SQL. 我以为我可以使用string.IsNullOrEmpty和LINQ to SQL。

Replace string.Empty with "" . string.Empty替换为"" Sadly, EF does not support string.Empty . 可悲的是,EF不支持string.Empty

EF LINQ support is very bad in general. EF LINQ支持一般非常糟糕。 Always be aware of this problem. 始终要注意这个问题。 It is a common cause of grief with EF. 这是EF的悲伤的常见原因。

LINQ to SQL does not have problems with common language idioms. LINQ to SQL没有常见语言习语的问题。

Btw, you can rewrite this much more nicely : c.Address.Line1 ?? "" 顺便说一句,你可以更好地重写这个: c.Address.Line1 ?? "" c.Address.Line1 ?? "" . c.Address.Line1 ?? ""

(string.IsNullOrEmpty(c.Address.Line1) ? string.Empty : c.Address.Line1)

is being translated into 正在翻译成

IIF(IsNullOrEmpty(c.Address.Line1), Invoke(value(System.Func`1[System.String])), c.Address.Line1)

All you're doing, is setting a string value to "" if it's null or "" already. 您正在做的只是将字符串值设置为""如果它已经为null或""

You should try to just use Line1 = c.Address.Line1 您应该尝试使用Line1 = c.Address.Line1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM