Custom mapping in Dapper

后端 未结 3 874
别跟我提以往
别跟我提以往 2021-02-04 12:03

I\'m attempting to use a CTE with Dapper and multi-mapping to get paged results. I\'m hitting an inconvenience with duplicate columns; the CTE is preventing me from having to Na

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 12:42

    The below code should work fine for you to load a list of sites with associated locations

    var conString="your database connection string here";
    using (var conn =   new SqlConnection(conString))
    {
        conn.Open();
        string qry = "SELECT S.SiteId, S.Name, S.Description, L.LocationId,  L.Name,L.Description,
                      L.ReportingId
                      from Site S  INNER JOIN   
                      Location L ON S.SiteId=L.SiteId";
        var sites = conn.Query
                         (qry, (site, loc) => { site.Locations = loc; return site; });
        var siteCount = sites.Count();
        foreach (Site site in sites)
        {
            //do something
        }
        conn.Close(); 
    }
    

提交回复
热议问题