user751651
user751651

Reputation:

Can an Dapper query to changed to an ObservableCollection?

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?

Upvotes: 0

Views: 1240

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064204

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

Upvotes: 2

Related Questions