[英]Using Dapper in method context to return multiple lists
I have a vendor class like this:我有一个这样的供应商类:
public class Vendor: DataAccess
{
//properties
public int VEND_ID;
public string VEND_NAME { get; set; }
public string VEND_ADDRESS { get; set; }
public string VEND_PHONE { get; set; }
public string VEND_WEBSITE { get; set; }
public string NOTES { get; set; }
public static string ErrorMessage { get; set; }
//Constructors
public Vendor(string name,string address,string phone,string website,string notes)
{
VEND_NAME = name;
VEND_ADDRESS = address;
VEND_PHONE = phone;
VEND_WEBSITE = website;
NOTES = notes;
}
public Vendor(string name)
{
VEND_NAME = name;
}
public Vendor() { }
)
I have an Orders Class like this:我有一个这样的订单类:
public class Order: DataAccess
{
//properties
public int ORDER_ID { get; set; }
public Vendor VENDOR_ID { get; set; }
public string ENTRY_DATE { get; set; }
public string ORDER_NO { get; set; }
public string TOTAL_COST { get; set; }
public string STATUS { get; set; }
public string NOTES { get; set; }
public string ATTACH_ID { get; set; }
public static string ErrorMessage { get; set; }
//constructors
//public Order(string )
}
My Data Access Class has the following method:我的数据访问类有以下方法:
public Tuple<List<Vendor>, List<Order>> GetVendorByName(string name)
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("TESTDB")))
{
using (var multi = connection.QueryMultiple("dbo.GetVendor_ByName", new { VEND_NAME = name }))
{
List<Vendor> vd=new List<Vendor>();
vd.Add(multi.Read<Vendor>().First());
List<Order> od = new List<Order>();
od.Add(multi.Read<Order>().Single());
var output = Tuple.Create(vd, od);
return output;
}
//var output = connection.QueryMultiple<Vendor>("dbo.GetVendor_ByName", new { VEND_NAME = name }).ToList();
}
}
The main class uses it like this:主类像这样使用它:
List<Vendor> vendor = new List<Vendor>();
private void _search_Click(object sender, EventArgs e)
{
Vendor db = new Vendor(cb_vendor.Text);
vendor=db.GetVendorByName(cb_vendor.Text);//This is where the error is pointed at
//dgv_data.Refresh();
dgv_data.DataSource = vendor;
}
I am trying to get the data from the SQL procedure to show in the DataGridView (dgv_data).我试图从 SQL 过程中获取数据以显示在 DataGridView (dgv_data) 中。 There is a one to many relation between Vendor and Order.
供应商和订单之间存在一对多的关系。 I want to get all the Orders for whatever Vendor Name is selected
我想获取所选供应商名称的所有订单
I know the issue is that I am returning List.我知道问题是我要返回 List。 I tried using Tuples but no joy, it gives me an error about implicit conversions.
我尝试使用元组,但没有任何乐趣,它给了我一个关于隐式转换的错误。 Any idea how to do what I am trying to accomplish?
知道如何做我想要完成的事情吗?
Error CS0029 Cannot implicitly convert type 'System.Tuple, System.Collections.Generic.List>' to 'System.Collections.Generic.List' PurchApp
错误 CS0029 无法将类型“System.Tuple, System.Collections.Generic.List>”隐式转换为“System.Collections.Generic.List” PurchApp
Update1:更新1:
The stored procedure is like this:存储过程是这样的:
ALTER proc [dbo].[GetVendor_ByName]
(
@VEND_NAME varchar(100)
)
as
Select Distinct a.VEND_NAME,b.* from dbo.Purch_Vendor a left join purch_order b on a.vendor_id=b.vendor_id where a.Vend_Name=@VEND_NAME
Try this.尝试这个。 It maps orders to the vendor on name.
它将订单映射到名称上的供应商。 I have added a property for List of Orders to the Vendor
我为供应商的订单列表添加了一个属性
public class Vendor : DataAccess
{
//properties
public int VEND_ID;
public string VEND_NAME { get; set; }
public string VEND_ADDRESS { get; set; }
public string VEND_PHONE { get; set; }
public string VEND_WEBSITE { get; set; }
public string NOTES { get; set; }
public List<Order> ORDERS { get; set; }
public static string ErrorMessage { get; set; }
//Constructors
public Vendor(string name, string address, string phone, string website, string
notes)
{
VEND_NAME = name;
VEND_ADDRESS = address;
VEND_PHONE = phone;
VEND_WEBSITE = website;
NOTES = notes;
}
public Vendor(string name)
{
VEND_NAME = name;
}
public Vendor() { }
}
public class Order : DataAccess
{
//properties
public int ORDER_ID { get; set; }
public Vendor VENDOR_ID { get; set; }
public string ENTRY_DATE { get; set; }
public string ORDER_NO { get; set; }
public string TOTAL_COST { get; set; }
public string STATUS { get; set; }
public string NOTES { get; set; }
public string ATTACH_ID { get; set; }
public static string ErrorMessage { get; set; }
//constructors
//public Order(string )
}
public Vendor GetVendorByName(string name)
{
using (IDbConnection connection = new
SqlConnection(Helper.CnnVal("TESTDB")))
{
var dict = new Dictionary<string, Vendor>();
var vendors = connection.Query<Vendor, Order, Vendor>
("dbo.GetVendor_ByName", (a, b) => Mapper(a, b, dict), new {
VEND_NAME = name }, splitOn: "ORDER_ID");
return dict.Values.FirstOrDefault()
}
}
public Vendor Mapper(Vendor vendor, Order order, Dictionary<string, Vendor> dict)
{
if (!dict.ContainsKey(vendor.VEND_NAME))
{
vendor.ORDERS = new List<Order>();
dict.Add(vendor.VEND_NAME, vendor);
}
var currentVendor = dict[vendor.VEND_NAME];
currentVendor.ORDERS.Add(order);
return currentVendor;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.