如何通过列和数字列对数据表进行分组

时间:2016-02-02 17:44:01

标签: c# entity-framework linq datatable

我正在尝试通过列对数据表进行分组并对数字字段求和,但我无法进行求和。这是我的代码,由数据表组合。

<!DOCTYPE html>
<html>
<head>
    <base href="http://demos.telerik.com/kendo-ui/donut-charts/index">
    <style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style>
    <title></title>
    <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.1.112/styles/kendo.common-material.min.css" />
    <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.1.112/styles/kendo.material.min.css" />

    <script src="//kendo.cdn.telerik.com/2016.1.112/js/jquery.min.js"></script>
    <script src="//kendo.cdn.telerik.com/2016.1.112/js/kendo.all.min.js"></script>
</head>
<body>
<div id="example">
    <div class="demo-section k-content wide">
        <div id="chart" style="background: center no-repeat url('../content/shared/styles/world-map.png');"></div>
    </div>
    <script>
        function createChart() {
            $("#chart").kendoChart({
                title: {
                    position: "bottom",
                    text: "Share of Internet Population Growth"
                },
                legend: {
                    visible: false
                },
                chartArea: {
                    background: ""
                },
                seriesDefaults: {
                    type: "donut",
                    startAngle: 150
                },
                series: [{
                    name: "2012",
                    data: [{
                        category: "Asia",
                        value: 53.8,
                        color: GetColor
                    },{
                        category: "Europe",
                        value: 16.1,
                        color: GetColor
                    },{
                        category: "Latin America",
                        value: 11.3,
                        color: GetColor
                    },{
                        category: "Africa",
                        value: 9.6,
                        color: GetColor
                    },{
                        category: "Middle East",
                        value: 5.2,
                        color:GetColor
                    },{
                        category: "North America",
                        value: 3.6,
                        color: GetColor
                    }],
                    labels: {
                        visible: true,
                        background: "transparent",
                        position: "outsideEnd",
                        template: "#= category #: \n #= value#%"
                    }
                }],
                tooltip: {
                    visible: true,
                    template: "#= category # (#= series.name #): #= value #%"
                }
            });
        }

        $(document).ready(createChart);
        $(document).bind("kendo:skinChange", createChart);

      var color = "#63C6FF";
      var percent = -20;
      function GetColor(val)
      {

            var R = parseInt(color.substring(1,3),16);
            var G = parseInt(color.substring(3,5),16);
            var B = parseInt(color.substring(5,7),16);

            R = parseInt(R * (100 + percent) / 100);
            G = parseInt(G * (100 + percent) / 100);
            B = parseInt(B * (100 + percent) / 100);

            R = (R<255)?R:255;  
            G = (G<255)?G:255;  
            B = (B<255)?B:255;  

            var RR = ((R.toString(16).length==1)?"0"+R.toString(16):R.toString(16));
            var GG = ((G.toString(16).length==1)?"0"+G.toString(16):G.toString(16));
            var BB = ((B.toString(16).length==1)?"0"+B.toString(16):B.toString(16));

            color = "#"+RR+GG+BB;
            return color;
      }
    </script>
</div>


</body>
</html>

我有一些名为quantity_received和damage_received的列。我需要总结这两列。如何在上面的代码中总结它们?

1 个答案:

答案 0 :(得分:0)

您有一个实现IEnumerable<DataRow>的分组。因此,在该分组中,您可以使用所有LINQ扩展方法,例如Sum()

DataTable dt_grouped_by = dt_data.AsEnumerable()
                          .GroupBy(r => new 
                                        { 
                                            LOCNR = r.Field<int>("LOCNR"),
                                            DEPTNR = r.Field<int>("DEPTNR")
                                        })
                          .Select(g => new
                          {
                               Code = r.First().Field<string>("CODE"),
                               SumQr = r.Sum(x => x.Field<int>("quantity_received"))
                               SumDr = r.Sum(x => x.Field<int>("damage_received"))
                          })
                          .OrderBy(x => x.Code)
                          .CopyToDataTable();

如您所见,我更喜欢将Field()扩展方法添加到行索引器row["x"],因为它是强类型的。当然我必须猜测实际的数据类型。我还删除了First(),因为您无法将一个DataRow转换为DataTable