使用linq返回对象列表,其中属性之和为列表列表中的Max()

时间:2019-10-02 09:52:44

标签: c# list linq

我有一个对象列表(List<List<Product>>)的列表。我只想返回一个列表列表,其中内部列表中sum的{​​{1}}最高。

我正在尝试在C#中使用linq查询来实现此目的。不过,对这些linq查询不是很熟悉。

cost

我想从条件成立的可用列表中返回一个列表。附带的代码返回空值。

谢谢

3 个答案:

答案 0 :(得分:0)

如果我没看错,您正在寻找ArgMax参数,使集合达到其 Maximum 值), 标准Linq 没有提供这种方法,但是可以在Aggregate的帮助下进行仿真:

List<List<Product>> data = ...

List<Product> result = data
  .Select(list => new {
     list,                                    // list
     max = list
       .Where(product => product.Factor < 10) 
       .Sum(product => product.cost)          // with criterium to maximize
   })
  .Aggregate((s, a) => s.max > a.max ? s : a) // ArgMax implementation
  .list;                                      // We want list only (without max) 

答案 1 :(得分:0)

尝试一下:

List<Product> list1 = new List<Product>() { new Product(1), new Product(3), new Product(2), new Product(6), new Product(5)};
        List<Product> list2 = new List<Product>() { new Product(14), new Product(10), new Product(8), new Product(6), new Product(1) };
        List<Product> list3 = new List<Product>() { new Product(11), new Product(22), new Product(38), new Product(14), new Product(155) }; 
        List<List<Product>> products = new List<List<Product>>() { list1, list2, list3 }; //just some dummy data

        var highestSumOfPrices = products.Max(lp => lp.Sum(p => p.Cost));

        var result = products
            .FirstOrDefault(lp => lp.Sum(p => p.Cost) == highestSumOfPrices);

我们在这里做的是: 首先,我们以列表中所有价格中的最高者为准。 然后我们选择符合条件的第一个列表:列表产品成本的总和等于集合中所有列表中最高的。 希望这对您有用。 或者,您可以尝试:

var result = products.OrderByDescending(pl=>pl.Sum(p=>p.Cost)).First();

您可以按产品价格总和对列表进行排序(降序),然后得出第一个结果;这是更简单的解决方案,效果也不错,但我无法说出性能

答案 2 :(得分:0)

我用您的查询编写了此测试代码,它可以工作。

static void Test()
{
  // Test data
  var ListOfList = new List<List<Product>>();
  var list1 = new List<Product>();
  list1.Add(new Product { name = "item 1", factor = 1, cost = 10 });
  list1.Add(new Product { name = "item 2", factor = 5, cost = 20 });
  var list2 = new List<Product>();
  list2.Add(new Product { name = "item 3", factor = 30, cost = 50 });
  list2.Add(new Product { name = "item 4", factor = 4, cost = 40 });
  var list3 = new List<Product>();
  list3.Add(new Product { name = "item 5", factor = 2, cost = 100 });
  list3.Add(new Product { name = "item 6", factor = 3, cost = 150 });
  var list4 = new List<Product>();
  list4.Add(new Product { name = "item 5", factor = 2, cost = 60 });
  list4.Add(new Product { name = "item 6", factor = 3, cost = 5 });
  ListOfList.Add(list1);
  ListOfList.Add(list2);
  ListOfList.Add(list3);
  ListOfList.Add(list4);
  // Query
  var query = ( from list in ListOfList
                where list.Sum(x => x.factor) <= 10
                orderby list.Sum(x => x.cost) descending
                select list ).FirstOrDefault();
  // Display results
  if ( query != null )
    foreach ( var item in query )
      Console.WriteLine($"{item.name} cost {item.cost} with factor {item.factor}");
}

class Product
{
  public string name;
  public int factor;
  public int cost;
}

结果

item 5 cost 100 with factor 2
item 6 cost 150 with factor 3