How do I map multiple lists with dapper

你。 提交于 2019-12-02 03:09:47

I also had trouble coming to grips with the fact that Dapper doesn't do this automatically.

First, I'm not sure about comma-separated values for "splitOn." I thought you could only have one value there. So I have multiple columns in my result set named "ID" for example.

Second, to get the proper 1:N relationships you need to do an extra manual step. For example, I did a 2-table join of participants and their phone numbers. Then I had to do this:

private List<Participant> CollapseResultSet(List<Participant> rawdataset)
{
    List<Participant> ret = new List<Participant>();
    if (!rawdataset.Any())
    {
        return ret;
    }
    else
    {
        List<string> partIds = rawdataset.Select(p => p.ID).Distinct().ToList();
        foreach (string pId in partIds)
        {
            Participant tmp = rawdataset.Where(p => p.ID == pId).FirstOrDefault();
            tmp.PhoneNumbers = rawdataset.Where(p => p.ID == pId).Select(n => n.PhoneNumbers[0]).ToList();
            ret.Add(tmp);
        }
        return ret;
    }
}

Hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!