Using Dapper, how to map a string column to a generic list?

dapper dapper-extensions sql-server string-split

Question

I own the next model:

public class Model {
   public string Name { get; set; }
   public List<int> Numbers { get; set; }
}

And anSQL the following query gives a dataset with two nvarchar columns:

 Name |  Numbers
'foo' | '1,2,3,4'
'bar' | '4,17'

Is there a straightforward method to automatically allocate the query's results to aList<Model> using Dapper ?

Although I could partition the data manually in C# code using multi-mapping, I would want to find a more straightforward method.

1
3
1/21/2016 4:33:44 PM

Accepted Answer

I'm not sure whether this qualifies as "simpler," but it is a possibility:

public class Result
{
    public string Name { get; set; }
    public List<int> Numbers { get; set; }
}
public class DapperTests
{
    [Test]
    public void Test()
    {
        var conn = new SqlConnection(@"Data Source=.\sqlexpress; Integrated Security=true; Initial Catalog=mydb");

        conn.Open();

        var result = conn.Query<string, string, Result>(
                    "select Name = 'Foo', Numbers = '1,2,3' union all select Name = 'Bar', Numbers = '4,5,6'", (a, b) => new Result
                    {
                        Name = a,
                        Numbers = b.Split(',').Select(Int32.Parse).ToList()
                    }, splitOn: "*").ToList();

        Assert.That(result.Count, Is.EqualTo(2));
        Assert.That(result.FirstOrDefault(x => x.Name == "Foo").Numbers.Count, Is.GreaterThan(0));
        Assert.That(result.FirstOrDefault(x => x.Name == "Bar").Numbers.Count, Is.GreaterThan(0));
    }
}
3
1/21/2016 9:07:37 PM

Popular Answer

A multimapping alternative that is quite nasty

public class Result
{
    public string Name { get; set; }
    public List<int> NumberList { get; set; }
    public string Numbers { set { NumberList = value.Split(',').Select(Int32.Parse).ToList(); } }
}

public class DapperTests
{
    [Test]
    public void Test()
    {
        var conn = new SqlConnection(@"Data Source=.\sqlexpress; Integrated Security=true; Initial Catalog=mydb");

        conn.Open();

        var sql = @"
            select Name = 'Foo', Numbers = '1,2,3';
            select Name = 'Bar', Numbers = '4,5,6';";

        var expectedResults = 2;

        var results = new List<Result>();
        using (var multi = conn.QueryMultiple(sql))
        {
            for (int i = 0; i < expectedResults; i++)
            {
                results.Add(multi.Read<Result>().Single());
            }
        }

        Assert.That(results.Count, Is.EqualTo(2));

        Assert.That(results.FirstOrDefault(x => x.Name == "Foo").NumberList.Count, Is.GreaterThan(0));
        Assert.That(results.FirstOrDefault(x => x.Name == "Bar").NumberList.Count, Is.GreaterThan(0));
    }
}


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow