简体   繁体   English

使用 Dapper 一对多映射(一个对象的两个列表)

[英]Mapping one to many with Dapper (Two list with one object)

I have student object with two list.I want retrieve student list from stored procedure.How can i get student object list with mentioned two list using dapper.我有两个列表的学生对象。我想从存储过程中检索学生列表。如何使用 dapper 获取包含两个列表的学生对象列表。

public class Student { public int StudentId { get;公共类学生{ public int StudentId { get; set;放; } }

public string RefNo { get; set; }

public int StudentModeId { get; set; }

public string FullName { get; set; }

public string KnownName { get; set; }

public List<StudentAttachment> StudentAttachments { get; set; }
public List<StudentExpenses> StudentExpenceses { get; set; }

} }

You can use something called QueryMultiple .您可以使用称为QueryMultiple 的东西。

If you're trying to effectively return 2 lists from 1 sproc and then populate them into 2 separate lists you can do something like this:如果您试图有效地从 1 个 sproc 返回 2 个列表,然后将它们填充到 2 个单独的列表中,您可以执行以下操作:

 var parameters = new DynamicParameters();
 parameters.Add("@myParam1", myParam1);
 parameters.Add("@myParam2", myParam2);

 string myQuery = "Your query here";
 var yourFirstList = new List<YourListType>();
 var yourSecondList = new List<YourSecondListType>();

 using (var multi = db.QueryMultiple(myQuery, parameters))
 {
     yourFirstList = multi.Read<YourListType>().ToList();
     yourSecondList = multi.Read<YourSecondList>().ToList();
 }

You have to make sure you know the order in which your result sets are coming back to be able to do this ie your first list is the first one and the second list is your second list type.您必须确保知道返回结果集的顺序才能执行此操作,即您的第一个列表是第一个列表,第二个列表是您的第二个列表类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM