none
How to return user type from linq query RRS feed

  • Question

  • All,

    I have a type SortableBindingList<ClassDAO> that I fill from the db.  When I run any query like lst.Where(p=>p.Name = John).ToList() the list always comes back as a simple list.  How do I get the results back as the original SortableBindingList?


    Thanks JD LinkedIn: www.linkedin.com/in/johndsanders

    Friday, May 4, 2012 3:26 AM

Answers

  • Hi John,

    The standard LINQ extension methods don't include a way to return a SortableBindingList. That means you'd need to instantiate a new one yourself based on the results of your LINQ query, or invert your condition and remove the items which you don't want from the existing list. You could add your own extension method to simplify the creation of a new list:

    namespace System.Linq
    {
        public static class LinqExtensions
        {
            public static SortableBindingList<T> ToSortableBindingList<T>(this IEnumerable<T> enumerable)
            {
                return new SortableBindingList<T>(enumerable.ToList());
            }
        }
    }

    Then to use this you'd simply replace the .ToList() with .ToSortableBindingList():

    lst.Where(p=>p.Name = John).ToSortableBindingList()

    Regards,

    Tyler

    • Proposed as answer by Tyler_A Friday, May 4, 2012 12:55 PM
    • Marked as answer by John D. Sanders Friday, May 4, 2012 2:12 PM
    Friday, May 4, 2012 9:02 AM

All replies

  • Hi John,

    The standard LINQ extension methods don't include a way to return a SortableBindingList. That means you'd need to instantiate a new one yourself based on the results of your LINQ query, or invert your condition and remove the items which you don't want from the existing list. You could add your own extension method to simplify the creation of a new list:

    namespace System.Linq
    {
        public static class LinqExtensions
        {
            public static SortableBindingList<T> ToSortableBindingList<T>(this IEnumerable<T> enumerable)
            {
                return new SortableBindingList<T>(enumerable.ToList());
            }
        }
    }

    Then to use this you'd simply replace the .ToList() with .ToSortableBindingList():

    lst.Where(p=>p.Name = John).ToSortableBindingList()

    Regards,

    Tyler

    • Proposed as answer by Tyler_A Friday, May 4, 2012 12:55 PM
    • Marked as answer by John D. Sanders Friday, May 4, 2012 2:12 PM
    Friday, May 4, 2012 9:02 AM
  • Awesome Tyler thank you very much.

    Thanks JD LinkedIn: www.linkedin.com/in/johndsanders

    Friday, May 4, 2012 12:34 PM