genxgeek
genxgeek

Reputation: 13357

LINQ/Projection - An expression tree may not contain a dynamic operation?

The code below is giving the following error:

"An expression tree may not contain a dynamic operation"

var x = db.Data.Select(x => new { name = TitleHT[x.TitleId], x.TitleId }).GroupBy(x => x.name);

TitleHT is a Dictionary<int, string> so that I can look up the string representation of the TitleId. As such I'm trying to assign the name to this string literal in the query . TitleId is of type int? not sure if that matters or not.

Upvotes: 0

Views: 2844

Answers (1)

Phil Klein
Phil Klein

Reputation: 7514

You tagged your question with "linq-to-sql" so I assume that the db is a L2S DataContext. In that case the LINQ Provider attempts to convert TitleHT[x.TitleId] to a SQL statement but is unable to. Something like this should work:

var x = db.Data
    .Select(x => x.TitleId)
    .ToList() // Subsequent projection using LINQ-to-Objects
    .Select(x => new { name = TitleHT[x], TitleId = x })
    .GroupBy(x => x.name);

Upvotes: 5

Related Questions