C#一对多关系

时间:2016-04-15 08:29:35

标签: c# mysql sql-server

我在SQL中有这个查询

SELECT p.Price, p.ID, p.Name, c.Name as CategoryName, p.CategoryID, c.ID AS CategoryPK,c.ParentID
,o.ID as OrderID,o.ProductID,o.ClientCompanyID,o.Quentity
FROM Products p LEFT JOIN Categories c ON p.CategoryID=c.ID LEFT JOIN Orders o ON o.ProductID=p.ID

这段代码

static List<Product> GetProducts(SqlCommand command)
{
    SqlDataReader reader = command.ExecuteReader();
    List<Product> listProducts = new List<Product>();
    while (reader.Read())
    {
        Product product = new Product();
        product.ID = reader.GetInt32(reader.GetOrdinal("ID"));

        product.Name = (!reader.IsDBNull(reader.GetOrdinal("Name")) ? (string)reader["Name"] : null);

        product.Price = reader.GetDecimal(reader.GetOrdinal("Price"));
        product.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
        Category newCat = new Category();
        newCat.ID = reader.GetInt32(reader.GetOrdinal("CategoryPK"));
        newCat.CategoryName = reader.GetString(reader.GetOrdinal("CategoryName"));
        newCat.Parent = (!reader.IsDBNull(reader.GetOrdinal("ParentID")) ? reader.GetInt32(reader.GetOrdinal("ParentID")) : 0);
        product.Category = newCat;

        Order order = new Order();
        order.ID = (!reader.IsDBNull(reader.GetOrdinal("OrderID")) ? (int?)reader["OrderID"] : null);
        order.ClientCompanyID = (!reader.IsDBNull(reader.GetOrdinal("ClientCompanyID")) ? (int?)reader["ClientCompanyID"] : null);
        order.ProductID = (!reader.IsDBNull(reader.GetOrdinal("ProductID")) ? (int?)reader["ProductID"] : null);
        order.Quentity = (!reader.IsDBNull(reader.GetOrdinal("Quentity")) ? (int?)reader["Quentity"] : null);

        List<Order> listOrders = new List<Order>();
        listOrders.Add(order);

        product.Orders = listOrders;
        listProducts.Add(product);
    }
    reader.Close();
    return listProducts;
}

问题是:当我从单个产品调用订单时,如何在不丢失任何订单的情况下删除重复项:

{
    foreach (var item in listProducts)
    {
        Console.WriteLine("Product Name : {0} Price : {1} Category : {2}", item.Name, item.Price, item.Category.CategoryName, item);

        foreach (var orders in item.Orders)
        {
            Console.WriteLine("Order : " + orders.Quentity + " Price : " + (float)item.Price * orders.Quentity);
        }
    }
}

我希望在致电product.order.id时显示此产品的所有订单。 我应该如何做到一对多关系?

产品|订单

产品A | ###### X

######### |。######ÿ

产品B | ###### Z

### | ######ķ

我的意思是产品A对于产品B可以有很多订单和相同的订单。 一对多如何褒奖。

1 个答案:

答案 0 :(得分:1)

创建C#对象时,只需检查产品是否已存在 像这样:

static List<Product> GetProducts(SqlCommand command)
{
    SqlDataReader reader = command.ExecuteReader();
    List<Product> listProducts = new List<Product>();
    while (reader.Read())
    {
        int productId = reader.GetInt32(reader.GetOrdinal("ID"));
        Product product = null;
        //check if product exists
        foreach (Product p in listProducts)
        {
           if(p.ID = productId)
               product = p;//product already exists
        }

       if(product == null)
       {
          //product doesn't exist, create new
          product = new Product();
          product.ID = productId;

          product.Name = (!reader.IsDBNull(reader.GetOrdinal("Name")) ? (string)reader["Name"] : null);

          product.Price = reader.GetDecimal(reader.GetOrdinal("Price"));
          product.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
          Category newCat = new Category();
          newCat.ID = reader.GetInt32(reader.GetOrdinal("CategoryPK"));
          newCat.CategoryName = reader.GetString(reader.GetOrdinal("CategoryName"));
          newCat.Parent = (!reader.IsDBNull(reader.GetOrdinal("ParentID")) ? reader.GetInt32(reader.GetOrdinal("ParentID")) : 0);
          product.Category = newCat;
          product.Orders = new List<Order>();
        }

        //add order to product
        Order order = new Order();
        order.ID = (!reader.IsDBNull(reader.GetOrdinal("OrderID")) ? (int?)reader["OrderID"] : null);
        order.ClientCompanyID = (!reader.IsDBNull(reader.GetOrdinal("ClientCompanyID")) ? (int?)reader["ClientCompanyID"] : null);
        order.ProductID = (!reader.IsDBNull(reader.GetOrdinal("ProductID")) ? (int?)reader["ProductID"] : null);
        order.Quentity = (!reader.IsDBNull(reader.GetOrdinal("Quentity")) ? (int?)reader["Quentity"] : null);
        product.Orders.Add(order);
        listProducts.Add(product);
    }
    reader.Close();
    return listProducts;
}

如果您的SQL结果是按产品排序的,那么您可以加快速度,然后检查它是否与最后一个产品不同,而不是检查当前产品是否已存在。 (如果不同的话,如果新产品不同,只需将订单添加到最后一个产品。)