如何将 List<B> 添加到对象 A,其中 List<B> 是 class A 的一部分
How to add List<B> to Object A, where List<B> is part of class A
我有两个 类 客户和产品,如下所示。我正在从数据库获取数据并将其读入 SqlDataReader。从那里我必须把它读给客户对象。我们将为每个客户提供多种产品。在这里,我必须将 Product 对象添加到 Customer 对象(我们可能为每个客户提供多种产品。请提出任何建议......最好的方法是什么?
public class Customer
{
public int CustomerId {get;set;}
public string Name {get;set;}
public List<Products> _products {get;set;}
}
public class Products
{
public int CustomerId {get;set;}
public int ProductId {get;set;}
public string Name {get;set;}
public int Quantity {get;set;}
}
While(dataReader.Read())
{
var _customer = new Customer{
CustomerId = (int)rdr["CustomerId"];
Name = (string)rdr["CustomerName"];
City = (string)rdr["City"];
};
var _product = new Products
{
CustomerId = (int)rdr["CustomerId"];
ProductId = (int)rdr["ProductId"];
Name = (string)rdr["ProductName"];
Quantity = (int)["Quantity"];
};
}
public List<Customer> GetCustomers(SqlConnection conn) {
using (conn);
conn.Open();
SqlCommand command = new SqlCommand("your_query;",conn);
// your query must return in order
// customerId, customerName, city, productId, productName, quantity
SqlDataReader reader = command.ExecuteReader();
var customers = new List<Customer>();
var products = new List<Product>();
while (reader.Read()) {
var customerId = reader.GetInt32(0);
var customer = new Customer() {
CustomerId = customerId,
Name = reader.GetString(1),
City = reader.GetString(2)
};
var existing = customers.Where(x => x.CustomerId == customerId).FirstOrDefault();
if (existing == null) {
customers.Add(customer);
}
var product = new Products
{
CustomerId = customerId,
ProductId = reader.GetInt32(3),
Name = reader.GetString(4),
Quantity = reader.GetInt32(5)
};
products.Add(product);
}
reader.Close();
return customers.ForEach(item => {
item.Products = products.Where(x => x.customerId == item.CustomerId).ToList();
});
}
}
如评论中所述,您需要使用 CustomerId
作为键将他们放入字典中,以跟踪您已经见过的客户。这是基本方法:
对于您读取的每条记录,首先从 reader 中获取 CustomerId
并检查该客户是否已经在字典中。如果是,则从字典中获取该客户对象;否则,从 reader 创建一个新客户并将其添加到字典中。然后,从 reader 中获取产品数据,创建一个新产品并将该产品添加到客户的产品列表中。
代码可能如下所示:
var customersById = new Dictionary<int, Customer>();
while (reader.Read())
{
int customerId = (int)reader["CustomerId"];
Customer customer;
if (!customersById.TryGetValue(customerId, out customer))
{
customer = new Customer
{
CustomerId = customerId,
Name = (string)reader["CustomerName"],
City = (string)reader["City"],
Products = new List<Product>()
};
customersById.Add(customerId, customer);
}
Product product = new Product
{
CustomerId = customerId,
ProductId = (int)reader["ProductId"],
Name = (string)reader["ProductName"],
Quantity = (int)reader["Quantity"]
};
customer.Products.Add(product);
}
然后,你可以像这样转储数据:
Console.WriteLine("Product list by customer:\n");
foreach (Customer cust in customersById.Values)
{
Console.WriteLine(string.Format("{0}) {1} of {2}", cust.CustomerId, cust.Name, cust.City));
foreach (Product prod in cust.Products)
{
Console.WriteLine(string.Format("\t{0}) {1} (qty {2})", prod.ProductId, prod.Name, prod.Quantity));
}
Console.Writeline();
}
Fiddle: https://dotnetfiddle.net/iO9vdM
我有两个 类 客户和产品,如下所示。我正在从数据库获取数据并将其读入 SqlDataReader。从那里我必须把它读给客户对象。我们将为每个客户提供多种产品。在这里,我必须将 Product 对象添加到 Customer 对象(我们可能为每个客户提供多种产品。请提出任何建议......最好的方法是什么?
public class Customer
{
public int CustomerId {get;set;}
public string Name {get;set;}
public List<Products> _products {get;set;}
}
public class Products
{
public int CustomerId {get;set;}
public int ProductId {get;set;}
public string Name {get;set;}
public int Quantity {get;set;}
}
While(dataReader.Read())
{
var _customer = new Customer{
CustomerId = (int)rdr["CustomerId"];
Name = (string)rdr["CustomerName"];
City = (string)rdr["City"];
};
var _product = new Products
{
CustomerId = (int)rdr["CustomerId"];
ProductId = (int)rdr["ProductId"];
Name = (string)rdr["ProductName"];
Quantity = (int)["Quantity"];
};
}
public List<Customer> GetCustomers(SqlConnection conn) {
using (conn);
conn.Open();
SqlCommand command = new SqlCommand("your_query;",conn);
// your query must return in order
// customerId, customerName, city, productId, productName, quantity
SqlDataReader reader = command.ExecuteReader();
var customers = new List<Customer>();
var products = new List<Product>();
while (reader.Read()) {
var customerId = reader.GetInt32(0);
var customer = new Customer() {
CustomerId = customerId,
Name = reader.GetString(1),
City = reader.GetString(2)
};
var existing = customers.Where(x => x.CustomerId == customerId).FirstOrDefault();
if (existing == null) {
customers.Add(customer);
}
var product = new Products
{
CustomerId = customerId,
ProductId = reader.GetInt32(3),
Name = reader.GetString(4),
Quantity = reader.GetInt32(5)
};
products.Add(product);
}
reader.Close();
return customers.ForEach(item => {
item.Products = products.Where(x => x.customerId == item.CustomerId).ToList();
});
}
}
如评论中所述,您需要使用 CustomerId
作为键将他们放入字典中,以跟踪您已经见过的客户。这是基本方法:
对于您读取的每条记录,首先从 reader 中获取 CustomerId
并检查该客户是否已经在字典中。如果是,则从字典中获取该客户对象;否则,从 reader 创建一个新客户并将其添加到字典中。然后,从 reader 中获取产品数据,创建一个新产品并将该产品添加到客户的产品列表中。
代码可能如下所示:
var customersById = new Dictionary<int, Customer>();
while (reader.Read())
{
int customerId = (int)reader["CustomerId"];
Customer customer;
if (!customersById.TryGetValue(customerId, out customer))
{
customer = new Customer
{
CustomerId = customerId,
Name = (string)reader["CustomerName"],
City = (string)reader["City"],
Products = new List<Product>()
};
customersById.Add(customerId, customer);
}
Product product = new Product
{
CustomerId = customerId,
ProductId = (int)reader["ProductId"],
Name = (string)reader["ProductName"],
Quantity = (int)reader["Quantity"]
};
customer.Products.Add(product);
}
然后,你可以像这样转储数据:
Console.WriteLine("Product list by customer:\n");
foreach (Customer cust in customersById.Values)
{
Console.WriteLine(string.Format("{0}) {1} of {2}", cust.CustomerId, cust.Name, cust.City));
foreach (Product prod in cust.Products)
{
Console.WriteLine(string.Format("\t{0}) {1} (qty {2})", prod.ProductId, prod.Name, prod.Quantity));
}
Console.Writeline();
}
Fiddle: https://dotnetfiddle.net/iO9vdM