none
How to return a System.Collections.Generic.List from IQuerable.select RRS feed

  • Question

  • Hi Guys,


    The question involves a bit of dyanmic link and gernerics so I hope I have asked it in the right forum.

    Anyway I have some code which returns a query from linq:

    Code Snippet

    public static List<TEntity> LoadMulti(DataContext dc, string WhereStatement, string SelectStatement, string OrderStatement) 

            {

                //  try

                //  {

                // problem..  How to use the select statement ?

     

                List<TEntity> entityList;

     

                Table<TEntity> table = dc.GetTable(typeof(TEntity)) as Table<TEntity>;

     

                if (WhereStatement == "")

                {

                    entityList = OrderStatement == "" ? table.ToList() : table.OrderBy(OrderStatement).ToList();

                }

                else if (SelectStatement == "")

                {

                    entityList = OrderStatement == "" ? table.Where(WhereStatement).ToList() : table.Where(WhereStatement).OrderBy(OrderStatement).ToList();

     

                 

                }

     

                else

                {

     

                    entityList = OrderStatement == "" ? table.Select(SelectStatement) : table.Select(SelectStatement).OrderBy(OrderStatement);

     

     

                }

     

                if (entityList == null)

                {

                    return null;

                }

     

                return entityList;

     

            }

     

     

     

    Which almost works fine but on the statment:

    table.Select(SelectStatement).OrderBy(OrderStatement);

    I get the error Error: 

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List<TEntity>'

    For both

    table.OrderBy(OrderStatement).ToList();

    table.Where(WhereStatement).ToList()

    Both have ToList() option which hadles the conversion for me.

    Table.Select does not have a ToList()

    So two part question:

    Why is there no ToList option for IQuerable.select

    And how can I convert 'System.Linq.IQueryable' to 'System.Collections.Generic.List<TEntity>'

     

     

    Wednesday, April 23, 2008 10:31 AM

Answers

  • You can't return List<TEntity> after you've used dynamic Select to produce objects that are not TEntity.  The dynamic API invented a new type at runtime to hold the results.  All you can do is make the return type of the function be IList instead of List<TEntity>.

     

    Friday, April 25, 2008 7:58 PM
    Moderator

All replies

  • The dynamic API's that accept string expressions for Where and Select do not return IQueryable<T> they return the non-generic IQueryable interface.  You'll have to cast to get back to IQueryable<T>.

     

     

    Wednesday, April 23, 2008 3:10 PM
    Moderator
  •  

    Thanks for your reply

     

    How would I cast back to IQueryable<T>. ?

     

     

     

    Wednesday, April 23, 2008 3:29 PM
  • Oh, I misread your question.  The Select function is changing the resulting sequences element type, so its not TEntity anymore.  You cannot return it as List<TEntity>. You have to return it as IList.

    Wednesday, April 23, 2008 6:45 PM
    Moderator
  • Thanks again for your repley

     

    I'm not sure how I would convert from an IQuerable to an IList.

     

    If I do something like:

     

    Code Snippet

    (IList<TEntity> ) table.Select(SelectStatement) ;

     

     

    It complies, but I when I run the application I get the error:

     

    Unable to cast object of type 'System.Data.Linq.DataQuery`1[DynamicClass1]' to type 'System.Collections.Generic.IList`1[TestDynamicLinq.C_COMPANY]'

     

    I understand that inheritance works different in Generics, but i'm not sure what do next

    Thursday, April 24, 2008 9:07 AM
  •  

    This almost works but

     

    Code Snippet

    Type dataClassType = dc.GetType();

    List<TEntity> myList =

    (

     

     

    table.Select(SelectStatement).Cast<TEntity>()

     

    ).ToList<TEntity>() ;

     

     

    entityList = myList.ToList();

     

     

    I get the error:

     

    No coercion operator is defined between types 'System.Int32' and 'TestDynamicLinq.C_COMPANY'

     

    Any ideas

    Thursday, April 24, 2008 4:54 PM
  •  

    return table.Select(selectstring).Cast<object>().ToList();

    Thursday, April 24, 2008 10:24 PM
    Moderator
  •  

    return table.Select(selectstring).Cast<object>().ToList();

     

    Works   but I need to return as a List of TEntity (not an object), so when I try

    Code Snippet

     

    List<TEntity> mylist =

    table.Select(SelectStatement).Cast<TEntity>().ToList()

     

     

    No coercion operator is defined between types 'System.Int32' and 'TestDynamicLinq.C_COMPANY'

     

    Any ideas


    Friday, April 25, 2008 4:14 PM
  • You can't return List<TEntity> after you've used dynamic Select to produce objects that are not TEntity.  The dynamic API invented a new type at runtime to hold the results.  All you can do is make the return type of the function be IList instead of List<TEntity>.

     

    Friday, April 25, 2008 7:58 PM
    Moderator
  • hi

    Cannot implicitly convert type 'System.Collections.Generic.List<SilverlightDataBindingWcf.EmployeeDataDataContext>' to 'System.Collections.Generic.List<SilverlightDataBindingWcf.Employee>'

     

    public

     

    List<Employee> GetByLastName(string LastName)

    {

     

    EmployeeDataDataContext emp = new EmployeeDataDataContext();

     

    var res = from e in emp.Employees

     

    where e.LastName.StartsWith(LastName)

     

    select emp;

     

    return res.ToList();

     

    }

     

    ; here i'm getting the above error

     

     

     

     

     

     

     

     

    Saturday, May 15, 2010 7:48 PM
  • hi

    Cannot implicitly convert type 'System.Collections.Generic.List<SilverlightDataBindingWcf.EmployeeDataDataContext>' to 'System.Collections.Generic.List<SilverlightDataBindingWcf.Employee>'

     

    EmployeeDataDataContext emp = new EmployeeDataDataContext();

    var res = from e in emp.Employees

    where e.LastName.StartsWith(LastName)  

    select emp;

     


    I guess you mean "select e" instead of "select emp".

    .


    Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools (add-in with new features for the L2S and EF designers in VS2008 and VS2010)
    huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)
    Sunday, May 16, 2010 1:31 AM
    Answerer