5

I have the following query, which is using Entity Framework.

Analytic firstSent = (from a in Repository.Query<Analytic>()
                      where a.EntityType == "Proposal" &&
                      a.EntityId == Program.ActiveProposal.Id &&
                      a.Marker == AnalyticMarker.EmailProposalUrl.ToString()
                      orderby a.TimestampUtc
                      select a).FirstOrDefault();

At run time, I get the following error:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

a.Marker is a string column, and AnalyticMarker.EmailProposalUrl is an enum value, and I want to compare that column against the name of that enum.

I understand that the conversion from an enum to a string is not supported by SQL, but why won't it resolve the value of this string and then pass the resulting string to SQL? That should work just fine.

4
  • 1
    sir, have you tried this? 1 2 Commented Jan 28, 2013 at 23:16
  • Can you cast and assign it to a string variable BEFORE the Query? Commented Jan 28, 2013 at 23:42
  • @DaveA: Yes, that's what FloydPink suggested. I just don't see why it should be necessary. Commented Jan 29, 2013 at 1:09
  • def not necessary. easier. I'm a fan of that! Commented Jan 29, 2013 at 1:30

2 Answers 2

5

Try this:

var emailProposalUrl = AnalyticMarker.EmailProposalUrl.ToString();
Analytic firstSent = (from a in Repository.Query<Analytic>()
                      where a.EntityType == "Proposal" &&
                      a.EntityId == Program.ActiveProposal.Id &&
                      a.Marker == emailProposalUrl
                      orderby a.TimestampUtc
                      select a).FirstOrDefault();

This other answer is explaining the reason why this could work as well..

The problem arises because ToString() isn't really executed, it is turned into a MethodGroup and then parsed and translated to SQL. Since there is no ToString() equivalent, the expression fails.

Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking about defining the string separately as you suggest, but I have several of these and it just seems a little awkward. What I don't understand is why LINQ isn't smart enough to resolve what it can in C# before constructing the SQL, similar to how the C# compiler resolves what constant expressions it can before generating code.
0

Well, in the places where C# will normally resolve tostring for you, it normally takes an object. Here, for example though, you're asking it to compare a string to an enum. Although C# COULD call tostring(), its not correct for it to do so.

Think about if it was int you were trying to compare against, rather than an enum. You can't compare the number 1 and a string. (Well, you can, but it involves assumptions, which is the point; the language is just trying to prod you into making the logic that little more explicit)

2 Comments

I think you need to reread my question. I'm trying to compare a string against a string.
fail That will teach me to try to answer questions as I'm going off to bed. Apologies!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.