locked
[EF 4.0]cast iqueryable to bindinglist<T> RRS feed

  • Question

  • Hi all.

    My simple question:

    var items = contex.MyView.Where(x => x.id == someID);

    foreach(var item in items)

    {

        if(item.SomeValue > 1)

        {

               var list = from row in items.Where(blah)

                     select new

                     {

                          id = row.ID,

                          someProp = row.SomeProp

                     };

                var bindingList = new BindingList<MyView>(list.ToList());

        }

    }

    This is the error:

    Argument 1: cannot convert from 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IList<MyContext.MyView>

    I need a bindinglist<T> to try a two way binding for a winforms application using a datagridview.

    Thank you all in advance.

    RaouL.

    Sunday, November 4, 2012 6:05 PM

Answers

  • Hi Raoul_BennetH;

    The reason for the error you are getting is the following:

    This query returns a IQueryable of type Anonymous 

    var list = from row in items.Where(blah)
               select new
               {
                   id = row.ID,
                   someProp = row.SomeProp
               };

    Then you attempt to create a BindingList<MyView> from the above query which is not allowed. Seeming that items in the above query is already a IQueryable<MyView> then you should be able to do the following.

    var list = from row in items.Where(blah)
               select row;
               
    var bindingList = new BindingList<MyView>(list.ToList());

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Alexander Sun Thursday, November 22, 2012 6:41 AM
    Sunday, November 4, 2012 6:59 PM