2013-05-07 42 views
3

我們遇到了在包含LINQ表達式中使用超過2100個元素的問題,所以我重寫了查詢以將之前用於包含比較的值插入IEnumerable類型的結構體中EnquiryID公開簡單int值(ID),並加入在此:LLBLGen TypedConstantExpression無法轉換爲SetExpression

IEnumerable<EnquiryID> enqIdList = ListToEnquiryIdList(enquiryIDs).ToList(); 

var extras = (from q in lmd.Quote 
join qe in lmd.QuoteExtra on q.Id equals qe.QuoteId 
join ei in enqIdList on q.EnquiryId equals ei.Id 
orderby qe.Created 
select new 
{ 
    EnquiryID = q.EnquiryId, qe.Created 
}).ToArray(); 

這產生此異常:

「無法轉換的類型‘System.Linq.Expressions.TypedConstantExpression’對象爲類型「SD .LLBLGen.Pro.LinqSupportClasses.ExpressionClasses.SetExpression'。「,這明顯是LLBLGEN指定的c

任何人有什麼想法?

+0

http://stackoverflow.com/questions/3132981/linq2sql-local-sequence-cannot-be-used-in-linq-to-sql-error 雅不能使用本地序列! – 2013-05-07 14:07:14

+0

請不要忘記提高我的答案! – Jay 2016-06-08 14:53:33

+0

不是說你不能,但它很難...看到我的回答 – Jay 2016-06-08 14:53:54

回答

1

可以訪問TypedConstantExpression的 '值' 屬性,像這樣:
(expression.Body爲MethodCallExpression).Object.GetType()的getProperty( 「值」)GetMethod.Invoke((expression.Body作爲。 MethodCallExpression).Object,NULL)

當表達由LambdaExpression()=>(null).GetType()

給出了完整的例子

static readonly Type TypedConstantExpressionType = Type.GetType("System.Linq.Expressions.TypedConstantExpression, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); 

     static readonly PropertyInfo TypedConstantExpressionValueProperty; 

     [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized | System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] 
     static SymbolExtensions() 
     { 
      TypedConstantExpressionValueProperty = IntrospectionExtensions.GetTypeInfo(TypedConstantExpressionType).GetProperty("Value"); 
     } 

     /// <summary> 
     /// Given a lambda expression that expressed a new object, returns the <see cref="System.Reflection.TypeInfo"/> of what type was expected to be allocated 
     /// </summary> 
     /// <param name="expression"></param> 
     /// <returns></returns> 
     public static TypeInfo GetTypeInfo(Expression<Action> expression) //Expression<Action> allows the syntax() => where Expression would require a Delgate. 
     { 
      Expression body = expression.Body; 

      if (body is NewExpression) 
      { 
       NewExpression newExpression = expression.Body as NewExpression; 

       return IntrospectionExtensions.GetTypeInfo((expression.Body as NewExpression).Constructor.DeclaringType); 
      } 
      else if (body is MemberExpression) 
      { 
       MemberExpression memberExpression = body as MemberExpression; 

       return IntrospectionExtensions.GetTypeInfo(memberExpression.Member.DeclaringType); 
      } 
      else if (body is MethodCallExpression) 
      { 
       MethodCallExpression methodCallExpression = expression.Body as MethodCallExpression; 

       if (methodCallExpression.Object is MemberExpression) 
       { 
        return IntrospectionExtensions.GetTypeInfo((methodCallExpression.Object as MemberExpression).Member.DeclaringType); 
       } 

       //Actually a RuntimeType from a TypedConstantExpression... 
       return IntrospectionExtensions.GetTypeInfo((Type)TypedConstantExpressionValueProperty.GetMethod.Invoke(methodCallExpression.Object, null)); 
      } 

      throw new System.NotSupportedException("Please create an issue for your use case."); 
     } 

與下面的類測試和代碼:

public class MyTestClass 
     { 
      string m_Test; 

      public string Test 
      { 
       get { return m_Test; } 
       set { m_Test = value; } 
      } 
     } 

     public class MyTestClass<T> : MyTestClass 
     { 
      T Backing; 

      public T Property 
      { 
       [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] 
       get { return Backing; } 
       [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] 
       set { Backing = value; } 
      } 

      public T AnotherProperty 
      { 
       get; 
       set; 
      } 
     } 



System.Reflection.TypeInfo typeInfo = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.GetTypeInfo(() => new MyTestClass<int>()); 

      if (typeInfo.GetGenericArguments()[0] != typeof(int)) throw new System.Exception("Not correct type"); 

      System.Console.WriteLine("TypeInfo.Name" + typeInfo.Name); 

      System.Console.WriteLine("TypeInfo.MetadataToken" + typeInfo.MetadataToken); 

      if (typeInfo.GetGenericArguments()[0] != typeof(int)) throw new System.Exception("Not correct type"); 

      typeInfo = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.GetTypeInfo(() => (typeof(MyTestClass<int>)).GetType()); 

      if (typeInfo.GetGenericArguments()[0] != typeof(int)) throw new System.Exception("Not correct type"); 

      System.Type unboundedType = typeof(MyTestClass<>); 

      typeInfo = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.GetTypeInfo(() => (unboundedType).GetType()); 

      System.Console.WriteLine("TypeInfo.Name" + typeInfo.Name); 

      System.Console.WriteLine("TypeInfo.MetadataToken" + typeInfo.MetadataToken); 
+0

正如ConstantExpression類可能會工作,但你的問題具體與TypedConstantExpression不相關,雖然在大多數情況下我相信它們確實是可以互換的。 https://msdn.microsoft.com/en-us/library/system.linq.expressions.constantexpression(v=vs.110).aspx – Jay 2016-06-09 16:04:52

+0

可以在此處找到完整示例的鏈接(包括單元測試) https://net7mma.codeplex.com/SourceControl/latest#Common/Extensions/SymbolExtensions.cs – Jay 2016-06-09 18:48:48

相關問題