locked
Invoke Func<System.Data.Objects.ObjectContext, ... > using Expression<TDel> RRS feed

  • Question

  • Hello!

    Here's two methods that I want to show you:
     private object Test(bool? a)
        {
          Expression<Func<List<int>, int, object>> __e = (x, y) => x.Where(z => z * z > 2*y);
          Func<List<int>, int, object> __d = __e.Compile();
          List<int> array = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
          object o;
          if (a.HasValue && a.Value)
            o = (__d as Delegate).DynamicInvoke(array, 4);
          else if (a.HasValue && !a.Value)
            o = (__d as Delegate).DynamicInvoke(new object[] { array, 4 });
          else
            o = __d.Invoke(array, 4);
          return o;
        }
    
    and
    private object Test2(bool? a)
        {
          Expression<Func<NaviModel.NaviEntities, int, object>> __e = (x, y) => x.TracStatus.Where(z => z.TracStatus_ID > y);
          Func<NaviModel.NaviEntities, int, object> __d = __e.Compile();
          ServiceDataManager sdm = GetServiceDataManager();
          object o ;
          if (a.HasValue && a.Value)
            o = (__d as Delegate).DynamicInvoke(sdm.ObjectContext, 4);
          else if (a.HasValue && !a.Value)
            o = (__d as Delegate).DynamicInvoke(new object[] { sdm.ObjectContext, 4 });
          else
            o = __d.Invoke(sdm.ObjectContext, 4);
          return o;
        }
    
    NaviModel.NaviEntities is object context generated by VS for my DB.

    The question is: Why invoking delegate in Test() method compiles Lambda expression as it shown in code (Test(any argument).predicate.base.base.Target.Lambda is  {z => ((z * z) > (2 * y))} )
    and at the same time invoking Test2() method compiles Lambda expression as : 
    Test2(any argument).base.System_Linq_Iquerable_Expression is {value(System.Data.Objects.ObjectQuery`1[NaviModel.TracStatus]).Where(z => (z.TracStatus_ID > Convert(value(System.Object[])[0]).Value))}  ???

    The problem is LINQ to Entities does not support arrays in theirs expressions...

    So what can I do to prevent converting my arguments in Test2() method ? 
    • Moved by nobugzModerator Monday, September 7, 2009 9:11 AM (From:Common Language Runtime)
    Monday, September 7, 2009 8:25 AM

Answers

  • Well, this problem was solved...

    Just use AsEnumerable() with any ObjectQuery<T> property in your EDM...
    And Lambda expression of delegate will not be changed with converting its arguments.

    Expression<Func<NaviModel.NaviEntities, int, object>> __e = (x, y) => x.TracStatus.AsEnumerable().Where(z => z.TracStatus_ID > y);

    Good luck.
    Thursday, September 10, 2009 8:58 PM