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). 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. 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
Update1:
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.