views:

42

answers:

1
using(SampleEntities entities = new SampleEntities()) {
var userMap = from ad in entities.SampleGroupsSet
              from uid in distinctUserIDs
              where ad.ShortUserID.ToLower() == uid.ToLower()
              select new {shortID = uid, longID = ad.UserID};

string userID = "sampleId";
var longIDs = from map in userMap
              where map.shortID.ToLower() == userID.ToLower()
              select map.longID;

if (longIDs != null && longIDs.Count() > 0)
{
    ...
}
...

I'm running into an issue where if I am querying for the Count of longIDs I'm getting an exception:

"Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."

Has anyone encountered this? Thanks.

+1  A: 

You have two issues. This:

uid.ToLower()

...can't be converted to SQL. This:

          where map.shortID.ToLower() == userID.ToLower()

Is the wrong way to do a case-insensitive restriction. It defeats using an index, and causes the issue you reference. Instead, do:

          where map.shortID.Equals(userID, StringComparison.OrdinalIgnoreCase) // or whatever...

Second issue: You seem to be trying to do a "where in". But this is the wrong way. In EF 4, you do:

where distinctUserIDs.Contains(ad.ShortUserID)

In EF 1 it's more involved.

Craig Stuntz
Hmm, still getting the same issue. What do you mean it defeats using an index?
LB
Ah, missed the other issue (you have two). I'll update. `col.ToLower` is converted to SQL as `LOWER(col)`, which means an index on `col` can't be used.
Craig Stuntz
I don't think Contains is supported with L2E... am I correct? http://blogs.msdn.com/alexj/archive/2009/03/26/tip-8-writing-where-in-style-queries-using-linq-to-entities.aspx
LB
Appreciate the help though, hopefully I can find a solution to this starting with the first query.
LB
Yeah, .Contains is not supported: http://msdn.microsoft.com/en-us/library/bb738638.aspx
LB
Did you read what I wrote? In EF 4 you can use contains. In EF 1 you can't (as your link confirms... *for EF 1 only*).
Craig Stuntz