本文介绍了string.IsNullOrEmpty +实体框架5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我使用了实体框架以来,已经有一段时间了,而且我正在回到EF 5,但是不会这样查询:

  SELECT 
c.OrganizationName as CompanyName,
c.OrganizationKey as CompanyId,
ISNULL(a.Line1,'')as Line1,
ISNULL( a.Line2,'')作为Line2,
a._CityStateZip as CityStateZip
FROM组织c
JOIN寻址一个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)
其中!c.IsSuspended
其中c.IsActive
选择新
{
CompanyName = c.OrganizationName,
CompanyId = c.OrganizationKey,
Line 1 =(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代码时,我收到以下错误:

 无法翻译表达式
'Table(Organization).Where(c => c.OrganizationName
.StartsWith(Invoke值(System.Func`1 [System.String]))))
.Where(c => Not(c.IsSuspended))
.Where(c => c.IsActive)
。选择(c => new<> f__AnonymousType2`5(
CompanyName = c.OrganizationName,
CompanyId = c.OrganizationKey,
Line1 = IIF(IsNullOrEmpty .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和不能把它视为当地的表达。

我完全错过了这里吗?我想我可以用LINQ to SQL使用string.IsNullOrEmpty。

解决方案

替换 string.Empty with 。令人遗憾的是,EF不支持 string.Empty



EF LINQ支持一般非常糟糕。总是意识到这个问题。这是EF的悲伤的常见原因。



LINQ to SQL没有常见语言习语的问题。



Btw,你可以更好地重写这个 c.Address.Line1?


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:

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:

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.

解决方案

Replace string.Empty with "". Sadly, EF does not support string.Empty.

EF LINQ support is very bad in general. Always be aware of this problem. It is a common cause of grief with EF.

LINQ to SQL does not have problems with common language idioms.

Btw, you can rewrite this much more nicely: c.Address.Line1 ?? "".

这篇关于string.IsNullOrEmpty +实体框架5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 17:18