Binding anonymous type to Create a BindingList

前端 未结 3 1952
执念已碎
执念已碎 2020-12-30 14:02

I am trying to create a BindingList<> from anonymous type returned by LINQ query but BindingList<> do not accept anonymous type, following is my code

v         


        
3条回答
  •  暖寄归人
    2020-12-30 14:08

    You can write an extension method:

    static class MyExtensions
    {
        public static BindingList ToBindingList(this IList source)
        {
            return new BindingList(source);
        }
    }
    

    and use it like this:

            var query = entities
                .Select(e => new
                {
                   // construct anonymous entity here
                })
                .ToList()
                .ToBindingList();
    

提交回复
热议问题