Can an Dapper query to changed to an ObservableCollection?

asp.net-mvc-4 dapper

Question

I'm tying to learn to use dapper.

I have this class here:

public class Member_Collection : ObservableCollection<Member>
{
}

and I have this method in my DAL class:

    public static Member_Collection SqlSelectAll(string connString)
    {
        Member_Collection  entityToReturn = null;

        using (var conn = new SqlConnection(connString))
        {
            var entityList = conn.Query("Select * From Member");

            entityToReturn = new Member_Collection();

            foreach (var item in entityList)
            {
                entityToReturn.Add(item);
            }
        }

        return entityToReturn;
    }

This there away for the query to return an ObservableCollection?

Accepted Answer

Getting it to "return" a specific collection type would be a case of adding a custom extension method. If you just wanted the generic observable collection type, then:

public static ObservableCollection<T> ToObservable<T>(
    this IEnumerable<T> source)
{
    return new ObservableCollection<T>(source);
}

Note that to return a specific subclass is more complicated. To use generics would require the caller to specify both generic arguments, which is vexing. You might need a per-item-type extension method - again, pretty ugly




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why