在LINQ查询中将枚举转换为字符串时出错

时间:2023-01-13 02:16:31

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.

LINQ to实体不承认方法的系统。字符串ToString()'方法,此方法不能转换为存储表达式。

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.

一个。标记是一个字符串列和分析标记。EmailProposalUrl是一个枚举值,我想将该列与枚举的名称进行比较。

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.

我理解SQL不支持从枚举到字符串的转换,但是为什么它不解析这个字符串的值,然后将结果字符串传递给SQL呢?这应该没问题。

2 个解决方案

#1


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.

问题出现了,因为ToString()并没有真正执行,它被转换为一个MethodGroup,然后被解析并转换为SQL。由于没有ToString()等效项,所以表达式失败。

#2


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.

在c#通常会为您解析tostring的地方,它通常会使用一个对象。这里,例如,您要求它将字符串与enum进行比较。虽然c#可以调用tostring(),但是这样做并不正确。

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)

想想如果它是int型的,而不是enum。你无法比较数字1和字符串。你可以,但它包含假设,这就是重点;语言只是试图促使你把逻辑变得更明确)

#1


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.

问题出现了,因为ToString()并没有真正执行,它被转换为一个MethodGroup,然后被解析并转换为SQL。由于没有ToString()等效项,所以表达式失败。

#2


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.

在c#通常会为您解析tostring的地方,它通常会使用一个对象。这里,例如,您要求它将字符串与enum进行比较。虽然c#可以调用tostring(),但是这样做并不正确。

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)

想想如果它是int型的,而不是enum。你无法比较数字1和字符串。你可以,但它包含假设,这就是重点;语言只是试图促使你把逻辑变得更明确)