无法使用两个列表中的项创建第三个列表

问题描述 投票:2回答:2

我正在尝试使用两个列表的组合值组合一个列表(让我们称之为FinalList):客户和产品。假设我们有四个客户和一个产品,FinalList应该有四个项目的最终结果(每个客户一个)。

例如:

Customer List:
Customer Code     |     Customer Name     |  Customer Branch ID
------------------|-----------------------|------------------------
001               |      Tom              |      T001                 
002               |      Dick             |      T002                 
003               |      Harry            |      T003                 
004               |      Jerry            |      T004                 


Product List:
Product Code      |    Product Name     
------------------|---------------------
  P001            |   Apple             

目前我正在尝试这样做:

var finalList = new List<ProductDetailDto>();
var customerList = new List<CustomerGroup>();

/// productsList is also type List<ProductDetailDto>();

for (var j = 0; j<= productsList.Count()-1; j++)
{
    for (int i = 0; i <= customerList.Count() - 1; i++)
    {
        var singleDetail = new ProductDetailDto();

        // Copy current products to groupDetail
        singleDetail = productsList[j];

        // Assemble rest of the info
        singleDetail.CustCode = customerList[i].Customer.CustomerCode;
        singleDetail.CustName = customerList[i].Customer.CustomerName;
        singleDetail.CustBranchId = customerList[i].Customer.CustomerBranchId;

        finalList.Add(singleDetail);
    }
}

return finalList;

但是,执行此操作后,finalList仅使用Jerry作为所有四个项目的客户。我尝试使用foreach也有相同的结果。我不太确定我在这里做错了什么,我很尴尬,这对某些人来说似乎是基本的,所以我希望有一双新鲜的眼睛来发现我在这里犯的错误......

另外,有什么方法可以进一步优化这个吗?

一如既往,将非常感谢任何帮助。谢谢。

c# for-loop foreach
2个回答
2
投票

这里:

// Copy current products to groupDetail
singleDetail = productsList[j];

您实际上并不复制当前产品,而是从productsList复制对项目的引用,并且在每个内部循环迭代中,您都会覆盖相同productsList[j]元素中的属性。

您可能想要阅读有关赋值如何在引用类型上工作的更多信息: https://www.microsoftpressstore.com/articles/article.aspx?p=2454676

如果要生成两个列表的叉积,则需要创建一个新对象:

var finalList = new List<ProductDetailDto>();
var customerList = new List<CustomerGroup>();

/// productsList is also type List<ProductDetailDto>();

for (var j = 0; j<= productsList.Count()-1; j++)
{
    for (int i = 0; i <= customerList.Count() - 1; i++)
    {
        var singleDetail = new ProductDetailDto 
        {
            ProductCode = productsList[j].ProductCode,
            ProductName = productsList[j].ProductName
            // and whatever other properties your product have
        };

        // Assemble rest of the info (these can actually go to object initializer too)
        singleDetail.CustCode = customerList[i].Customer.CustomerCode;
        singleDetail.CustName = customerList[i].Customer.CustomerName;
        singleDetail.CustBranchId = customerList[i].Customer.CustomerBranchId;

        finalList.Add(singleDetail);
    }
}

return finalList;

至于我,你在CustCode中拥有像CustNameCustBranchIdProductDetailDto这样的属性令人困惑。对于productsList中的对象,这些属性是否为空?考虑专门为CustomerProductDto这些需求创建另一个类,以便您的意图变得更加清晰。

您可以使用LINQ优化它:

var items = from p in productsList
            from c in customerList
            select new ProductDetailDto
            {
                ProductCode = p.ProductCode,
                ProductName = p.ProductName
                CustCode = c.Customer.CustomerCode,
                CustName = c.Customer.CustomerName,
                CustBranchId = c.Customer.CustomerBranchId,
            };
return items.ToArray();

2
投票

这行代码:

singleDetail = productsList[j];

影响指针而不是值,所以最后你有一个相同指针的列表,所以你只有最后一次修改重复customerList.Count()

所以你必须像customerList一样逐个添加值

© www.soinside.com 2019 - 2024. All rights reserved.