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:

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.

1
  • EF LINQ support is just sad... LINQ to SQL supports this (and much more).
    – usr
    Dec 19, 2012 at 19:17

2 Answers 2

11

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 ?? "".

2
  • 1
    That's exactly what I needed. Thanks also for the tip to use c.Address.Line1 ?? "" .... ?? completely slipped my mind! Dec 19, 2012 at 19:23
  • 1
    May be this answer is too old. But with EF6, it supports string.Empty also. Mar 30, 2016 at 5:43
1
(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.

You should try to just use Line1 = c.Address.Line1

1
  • Line1 = c.Address.Line1 returns the value null if Line1 is null. That's why I want to use string.IsNullOrEmpty. Dec 19, 2012 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.