wpf 使用 Dapper 映射嵌套对象列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32007238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Map lists of nested objects with Dapper
提问by puti26
I'm using Dapper and I have classes like this:
我正在使用 Dapper,我有这样的课程:
public class Article{
public int Id { get; set; }
public string Description{get;set;}
public Group Group { get; set; }
public List<Barcode> Barcode {get;set;}
...
}
public class Group{
public int Id { get; set; }
public string Description {get;set;}
}
public class Barcode{
public int Id { get; set; }
public string Code{get;set;}
public int IdArticle { get; set; }
...
}
I can get all information about Article but I would like to know if is possible with one query get also the list of barcodes for each article. Actually what I do is this:
我可以获得有关文章的所有信息,但我想知道是否可以通过一个查询获取每篇文章的条形码列表。其实我做的是这样的:
string query = "SELECT * FROM Article a " +
"LEFT JOIN Groups g ON a.IdGroup = g.Id ";
arts = connection.Query<Article, Group, Article>(query,
(art, gr) =>
{ art.Group = gr; return art; }
, null, transaction).AsList();
I also found a good explanation herebut I don't understand how to use it in my case, because I have also the Group class. How should I do this with Dapper, is it possible or the only way is to do different steps? Thanks
我也在这里找到了一个很好的解释,但我不明白如何在我的情况下使用它,因为我也有 Group 类。我应该如何使用 Dapper 执行此操作,是否可能或唯一的方法是执行不同的步骤?谢谢
采纳答案by von v.
QueryMultiple is your friend
QueryMultiple 是你的朋友
var query = @"
select a.*, g.* from Article a left join Groups g on g.Id = a.IdGroup
select * from Barcode";
//NOTE: IdGroup should exists in your Article class.
IEnumerable<Article> articles = null;
using (var multi = connection.QueryMultiple(query)){
articles = multi.Read<Article, Group, Article>((a, g)=>
{ a.Group = g; return a; });
if (articles != null) {
var barcodes = multi.Read<Barcode>().ToList();
foreach(var article in articles){
article.Barcode = barcodes.Where(x=>x.IdArticle = article.Id).ToList();
}
}
}
That may not be fun especially if you don't have any filters in your query. But I doubt that you will return all Articles. In that case you can filter the Barcode like this (edited sql) > select * from Barcode where Id in @ids. Then include the parameter ids(a list of Article Ids) in the QueryMultiple.
这可能并不有趣,特别是如果您的查询中没有任何过滤器。但我怀疑您是否会退回所有文章。在这种情况下,您可以像这样过滤条形码(编辑 sql)> select * from Barcode where Id in @ids。然后ids在 QueryMultiple 中包含参数(文章 ID 列表)。
Option2
选项2
Or you could just do separate queries:
或者你可以做单独的查询:
var query = "select a.*, g.* from Article a left join Groups g on g.Id = a.IdGroup";
var articles = connection.Query<Article, Group, Article>(query,
(a,g)=> { a.Group = g; return g; }).ToList();
query = "select * from Barcode where IdArticle IN @articleIds";
var articleIds = articles.Select(x=>x.Id);
var barcodes = connection.Query<Barcode>(query, new { articleIds });
foreach(var article in articles){
article.Barcode = barcodes.Where(x=>x.IdArticle = article.Id);
}
I prefer the first option.
我更喜欢第一个选项。

