![](/img/trans.png)
[英]System.NotSupportedException using DateTime? in Linq to Entities
[英]LINQ to Entities : “select new” expression tree throws System.NotSupportedException
我正在嘗試構建一個反映“選擇新”查詢的表達式樹。
我正在使用Ethan對這個問題的答案。 它對於普通列表非常有用,但是對於LINQ to Entities,我得到此異常:
System.NotSupportedException: Unable to create a constant value of type X.
Only primitive types or enumeration types are supported in this context.
其中X是我要查詢的實體。
使用調試器,這是帶有表達式樹的IQueryable:
SELECT [Extent1].[Id] AS [Id],
[Extent1].[Nombre] AS [Nombre],
[Extent1].[Apellido] AS [Apellido]
FROM [dbo].[Empleadoes] AS [Extent1]
.Select(t => new Nombre;String;() {Nombre = t.Nombre})
這是使用常規linq表示法的IQueryable(實際上不是相同的查詢,但要指出的是-它們是不同的)
SELECT [Extent1].[Dni] AS [Dni],
[Extent1].[Nombre] + N' ' + [Extent1].[Apellido] AS [C1]
FROM [dbo].[Empleadoes] AS [Extent1]
任何幫助表示贊賞。 謝謝。
通過查看動態LINQ代碼,我發現了問題。
Ethan的解決方案做到了:
source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable),
"Select",
new Type[] { source.ElementType, dynamicType },
Expression.Constant(source),
selector));
在Dynamic LINQ的Dynamic.cs中,它們執行以下操作:
source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable),
"Select",
new Type[] { source.ElementType, lambda.Body.Type },
source.Expression,
Expression.Quote(lambda)));
相關更改在對Expression.Call的調用的第4個參數中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.