locked
Select in System.Linq.Dynamic not working for the Entity Framework RRS feed

  • Question

  • I'm trying to use the LINQ Dynamic Query Library posted here :http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

      string, which is column name of that table and it will give me data in form of IList.

        public IList GetListByCriteriaQuery<T>(List<string> a_lstSelectColumns) where T : class
            {
                IList lst = null;
                try
                {
                    if (a_lstSelectColumns != null && a_lstSelectColumns.Count > 0)
                    {
                        var querableType = dbContextOfSqlSupport123.CreateObjectSet<T>().AsQueryable().Where(p => true)
                               .Select("new(" + string.Join(",", a_lstSelectColumns) + ")");
                             //  .OfType<T>().ToList<T>();
                    }
                }
                catch (Exception ex)
               {               

    return null;
                }
                return lst;
            }

    When i try to convert this Iquerable to List, using given code

     var querableType = dbContextOfSqlSupport123.CreateObjectSet<T>().AsQueryable().Where(p => true)
                               .Select("new(" + string.Join(",", a_lstSelectColumns) + ")");
                              .OfType<T>().ToList<T>();

    It gives me error as: Unable to cast the type 'DynamicClass1' to type ... LINQ to Entities only supports casting Entity Data Model primitive types. So how can i return an Ilist from this function ?

    If i modified my above given code to return IQueryable as written below:

     var querableType = dbContextOfSqlSupport123.CreateObjectSet<T>().AsQueryable().Where(p => true)
                               .Select("new(" + string.Join(",", a_lstSelectColumns) + ")");

    How can i retrieve value from this object without using reflection ?

    Tuesday, August 20, 2013 1:12 PM

Answers

  • Hello,

    From your description, I notice the issue you are experiencing is that when you try to convert IQuerable to List it throws an exception.

    If I have misunderstood, please let me know.

    With your code I run it on my PC and it throws the exception, too.

    So I changed the code a little, please see the code below:

    public IList GetListByCriteriaQuery<T>(TestDataBaseEntities1 db, List<string> a_lstSelectColumns) where T : class
            {
                IList lst = null;
                try
                {
                    if (a_lstSelectColumns != null && a_lstSelectColumns.Count > 0)
                    {
                        ObjectContext context = ((IObjectContextAdapter)db).ObjectContext;
                        lst = context.CreateObjectSet<T>().AsEnumerable().Where(p => true)
                              .Select("new(" + string.Join(",", a_lstSelectColumns) + ")").Cast<DynamicClass>().ToList();
                        //lst = context.CreateObjectSet<T>().AsQueryable().Where(p => true)
                        //      .Select("new(" + string.Join(",", a_lstSelectColumns) + ")").OfType<DynamicClass>().ToList();
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
                return lst;
            }

    I use the Cast<>() instead of OfType<>() and I use the AsEnumerable() rather than AsQueryable() because Cast<>( DynamicClass) is supported by enumeration types.

    And I re-write the Select() which is IEnumerable type and code is just like below:

    public static IEnumerable<T> Select<T>(this IEnumerable source, string selector, params object[] values)
            {
                return Select(source, selector, values).Cast<T>();
            }
            public static IEnumerable Select(this IEnumerable source, string selector, params object[] values)
            {
                if (source == null) throw new ArgumentNullException("source");
                if (selector == null) throw new ArgumentNullException("selector");
                LambdaExpression lambda = DynamicExpression.ParseLambda(source.AsQueryable().ElementType, null, selector, values);
                return source.AsQueryable().Provider.CreateQuery(
                    Expression.Call(
                        typeof(Queryable), "Select",
                        new Type[] { source.AsQueryable().ElementType, lambda.Body.Type },
                        source.AsQueryable().Expression, Expression.Quote(lambda)));
            }

    And the result:

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Fred Bao Thursday, August 22, 2013 6:40 AM
    • Marked as answer by Fred Bao Monday, August 26, 2013 7:33 AM
    Wednesday, August 21, 2013 9:25 AM