2016-01-29 147 views
-7

我对C#比较陌生。我想在明年的方式从列表中获取列表:C#+从列表中获取列表

模型数据:

string productId 
string productName 
string depotName 
int quantity 

List<Data> FullList 

例如:

PROD1 | product1 | depot1 | 1.00

prod1 | product1 | depot2 | 2.00

prod1 | product1 | depot3 | 0.00

prod2 | product2 | depot1 | 2.00

prod2 | product2 | depot2 | 5.00

我拿到名单喜欢在这之后,我想从这个在下的方式创建一个多个列表:

List<Data> NewList 

PROD1 | product1 | depot1,depot2 | 3.00

prod2 | product2 | depot1,depot2 | 7.00

我想将列表中的项目按product_id, sum(quantity)和concat字符串的库位名称分组。

我尝试,并得到一些点(分组和使用GroupBy(x => x.product_id)Distinct()得到不同的值,但我不能让sum(quantity)

有人可以帮助我。

非常感谢你。

+1

你觉得这个难以置信的帖子是个问题吗? – Eser

+0

对不起,什么是不可读的?你能看到字母,我写的文字......还是有问号或一些奇怪的数据? – user3200393

+0

发布后你看了你的问题吗?你在开玩笑吗?顺便说一句:我不是那个需要帮助的人。所以,傲慢的傲慢对你无能为力。 – Eser

回答

0
var summary = FullList 
    .GroupBy(p => p.product_id) 
    .Select(g => new { 
     product_id = g.product_id, 
     product_name = g.First().product_name, 
     quantity = g.Sum(p => p.quantity) 
    }) 
    .ToList(); 

作为练习思考让所有的车厂名左右Concat

+0

谢谢。你的帖子很干净,简短,给我答案,我寻找。 此外,我想感谢其他人的有用评论。 为了获取连接软件仓库名称,我没有使用Concat()。我以这种方式解决它: 'depotName = g.Select(q => q.depotName).Aggregate((e,r)=> e +“,”+ r)' – user3200393

+0

而且,因为我只需要depot数量多于0的名称,我补充说: 'depotName = g.Where(z => z.quantity> 0).Select(q => q.depotName).Aggregate((e,r)=> e +“,”+ r)' – user3200393

0

更新:

此实现不使用linq,但它做的工作正确。不知道有多好,存取大量数据时的性能,

代码背后:

List<Data> data = new List<Data>() 
      { 
       new Data() { productId = "prod1", productName = "product1", depotName = "depot1", quantity = 1 }, 
       new Data() { productId = "prod1", productName = "product1", depotName = "depot2", quantity = 2 }, 
       new Data() { productId = "prod1", productName = "product1", depotName = "depot3", quantity = 0 }, 
       new Data() { productId = "prod2", productName = "product2", depotName = "depot1", quantity = 2 }, 
       new Data() { productId = "prod2", productName = "product2", depotName = "depot2", quantity = 5 }, 
      }; 

      List<Data> newData = new List<Data>(); 

      foreach(var d in data) 
      { 
       Data newDataRecord = newData.FirstOrDefault(nd => nd.productId == d.productId); 
       if (newDataRecord != null) 
       { 
        int newDataRecordIndex = newData.IndexOf(newDataRecord); 
        if(!newDataRecord.depotName.Contains(d.depotName)) 
        { 
         newData[newDataRecordIndex].depotName += string.Format(",{0}", d.depotName); // Append to the existing depotName 
        } 
        newData[newDataRecordIndex].quantity += d.quantity; // Add quantity 
       } 
       else 
       { 
        newData.Add(d); 
       } 
      } 

型号:

public class Data 
    { 
     public string productId { get; set; } 
     public string productName { get; set; } 
     public string depotName { get; set; } 
     public int quantity { get; set; } 
    } 

输出:

PROD1 | product1 | depot1,depot2,depot3 | 3.00

prod2 | product2 | depot1,depot2 | 7.00

+0

您可以在没有任何解释的情况下获得downvotes回答。 “试试这个”真的没有用的文字回答... –

+0

好的谢谢你的提醒。我添加了一些信息 –