Many to Many in Linq using Dapper

 ̄綄美尐妖づ 提交于 2019-12-10 10:37:04

问题


I have Places, each place can have many tags. Each tag can be assigned to many places.

public class Place {
    public int Id { get; set; }
    public string PlaceName { get; set; }

    public IEnumerable<Tag> Tags { get; set; }
}

public class Tag {
    public int Id { get; set; }
    public string TagName { get; set; }
}

public class TagPlace {
    public int Id { get; set; }
    public PlaceId { get; set; }
    public TagId { get; set; }
}

The database has equivalent tables with foreign keys as appropriate.

I want to get a collection of Places, and I want each Place to have an appropriate colleciton of Tags. I guess using Linq might be required.

I've found various articles on this, but they aren't quite the same / deal with a list of ints rather than two collections of objects.

eg https://social.msdn.microsoft.com/Forums/en-US/fda19d75-b2ac-4fb1-801b-4402d4bd5255/how-to-do-in-linq-quotselect-from-employee-where-id-in-101112quot?forum=linqprojectgeneral

LINQ Where in collection clause

What's the best way of doing this?


回答1:


The classical approach with Dapper is to use a Dictionary to store the main objects while the query enumerates the records

public  IEnumerable<Place> SelectPlaces()
{
    string query = @"SELECT p.id, p.PlaceName, t.id, t.tagname
                     FROM Place p INNER JOIN TagPlace tp ON tp.PlaceId = p.Id
                     INNER JOIN Tag t ON tp.TagId = t.Id";
    var result = default(IEnumerable<Place>);
    Dictionary<int, Place> lookup = new Dictionary<int, Place>();
    using (IDbConnection connection = GetOpenedConnection())
    {
         // Each record is passed to the delegate where p is an instance of
         // Place and t is an instance of Tag, delegate should return the Place instance.
         result = connection.Query<Place, Tag, Place(query, (p, t) =>
         {
              // Check if we have already stored the Place in the dictionary
              if (!lookup.TryGetValue(p.Id, out Place placeFound))
              {
                   // The dictionary doesnt have that Place 
                   // Add it to the dictionary and 
                   // set the variable where we will add the Tag
                   lookup.Add(p.Id, p);
                   placeFound = p;
                   // Probably it is better to initialize the IEnumerable
                   // directly in the class 
                   placeFound.Tags = new List<Tag>();
              }

              // Add the tag to the current Place.
              placeFound.Tags.Add(t);
              return placeFound;

          }, splitOn: "id");
          // SplitOn is where we tell Dapper how to split the record returned
          // in the two instances required, but here SplitOn 
          // is not really needed because "Id" is the default.

     }
     return result;
}


来源:https://stackoverflow.com/questions/50786514/many-to-many-in-linq-using-dapper

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