views:

31

answers:

1

How can I reformulate an Entity Data Model query to work around a parsing error? Here is the query:

    var selectedNotes = notes
        .Where(n => n.Tags.Count == 0)

And here is the exception that is being thrown:

There was an error parsing the query. [ Token line number = 12,Token line offset = 53,Token in error = AS ]

Here is the background to my problem:

I am working on an app that uses Entity Framework 4. The app organizes rich-text documents, called Notes, which are searchable by Tags, like blog entries. One of my Entity Data Model queries retrieves only Notes that have no Tags:

searchResults = DataStore.ObjectContext.Notes.WhereContainsNoTags();

WhereContainsNoTags() is written as a LINQ extension method, and it contains the lambda expression for the query:

public static IQueryable<Note> WhereContainsNoTags(this IQueryable<Note> notes)
{
    IQueryable<Note> results;

    // Select Notes that contain no search Tags
    var selectedNotes = notes
        .Where(n => n.Tags.Count == 0)
        .OrderBy(n => n.Title);
    results = selectedNotes;

    // Set return value
    return results;
}

For simplicity, I have omitted the try-catch block that wraps the lambda expression and the logging code that logs any errors.

Here is the odd part: The query runs fine on my development machine, but it throws the above exception on the test machine. I have several other queries (match all Tags, match any Tags, and so on) that run fine on both the development and test machines.

I am quessing that the exception relates to the SQL generated by the EDM query. Is that the case? What is the best workaround? Is there a way to reformulate the lambda expression to avoid the problem?

Thanks for your help.

+4  A: 
var selectedNotes = notes.Where(n => !n.Tags.Any())
Yakimych
Thanks, but that won't really help me--it will select Notes that do not contain any of a specified set of tags. I want to select Notes that have no Tags at all.
David Veeneman
Not sure what you mean by "a specified set of tags". Any() returns 'true' if there is at least one element in the collection and 'false' if there are 0 elements in the collection. Thus "!n.Tags.Any()" and "n.Tags.Count == 0" will always return the same value.
Yakimych
Now I get it! Thanks for the help. Accepted and +1.
David Veeneman