C#中集合的子总和

时间:2016-01-08 18:52:45

标签: c#

我在C#中有一组项目,有两个属性:productName和Volume。我想通过productname在这个集合上执行一个子集来获取Volumes。在SQL中它看起来像这样:

SELECT 
    productName, sum(Volume)
FROM collection
GROUP BY productName

我曾尝试使用LINQ,但它给出了Data没有GroupBy的错误:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Linq;

namespace PieChartDataTable
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<DataItem> sortedPieData = new ObservableCollection<DataItem>(); //creates the pie data for reference later
        Data pieData = new Data();

        public class DataItem : IComparable
        {
            public string productName { get; set; }
            public double VOLUME { get; set; }
            public static DataItem createDataItem(string productName, double VOLUME)
            {
                DataItem dataItem = new DataItem();
                dataItem.productName = productName;
                dataItem.VOLUME = VOLUME;
                return dataItem;
            }
            public int CompareTo(object obj)
            {
                DataItem dataItem = obj as DataItem;
                if (dataItem == null)
                {
                    throw new ArgumentException("Object is not a DataItem.");
                }
                return this.VOLUME.CompareTo(dataItem.VOLUME);
            }
        }

        public class Data : ObservableCollection<DataItem>
        {

            public Data()
            {
                Add(new DataItem { productName = "productName 1", VOLUME = 5 });
                Add(new DataItem { productName = "productName 2", VOLUME = 6 });
                Add(new DataItem { productName = "productName 3", VOLUME = 3 });
                Add(new DataItem { productName = "productName 3", VOLUME = 3 });
                Add(new DataItem { productName = "productName 3", VOLUME = 3 });
                //Add(new DataItem { productName = "productName 4", VOLUME = 7 });
                //Add(new DataItem { productName = "productName 5", VOLUME = 5 });
            }


            public ObservableCollection<DataItem> sortCollection()
            {
                ObservableCollection<DataItem> objSort = new
                    ObservableCollection<DataItem>(this.OrderBy(DataItem =>
                        DataItem.VOLUME));
                return objSort;
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
            var result = Data.GroupBy(item => DataItem.productName)
             .Select(group =>
                    new DataItem
                    {
                        productName = group.Key,
                        VOLUME = group.Sum(item => DataItem.VOLUME)
                    })
             .ToList();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {

            sortedPieData = pieData.sortCollection();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

假设你的班级是这样的:

public class Item
{
    public string productName { get; set; }
    public int Volume { get; set; }
}

你需要做这样的事情:

var result = input
             .GroupBy(item => item.productName)
             .Select(group =>
                    new Item
                    {
                        productName = group.Key,
                        Volume= group.Sum(item => item.Volume)
                    })
             .ToList();

答案 1 :(得分:1)

您需要一个类

的实例
            Data data = new Data();
            var result = data.GroupBy(item => item.productName)
             .Select(group =>
                    new DataItem
                    {
                        productName = group.Key,
                        VOLUME = group.Sum(item => item.VOLUME)
                    })
             .ToList();​