DataTable使用group by计算 [英] DataTable compute with group by

查看:540
本文介绍了DataTable使用group by计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友..



我需要帮助 - 这是我的问题:

我有一个数据表,我正在使用查找总和的计算方法

它看起来像这样:

Friends..

I need a help - here is my problem:
I have a Data Table and i am using the Compute method to find the sum
It looks like this:

DataTable dTable = new DataTable();
        dTable.Columns.Add("AutoID");
        dTable.Columns.Add("Name");
        dTable.Columns.Add("Address");
        DataRow row = null;
        for (int i = 0; i < 5; i++)
        {
            row = dTable.NewRow();
            row["AutoID"] = i + 1;
            row["Name"] = i + " - Ram";
            row["Address"] = "Ram Nagar, India";
            dTable.Rows.Add(row);
        }
        dTable.Rows.Add(6, "Manual Data - 1", "Manual Address - 1, USA");
        dTable.Rows.Add(7, "Manual Data - 2", "Manual Address - 2, USA");

        double sum = Convert.ToDouble(dTable.Compute("SUM(AutoID)", ""));


//Upto here its fine...

        object sum1 = dTable.Compute("SUM(AutoID)", "group by Address");
 //this makes error like 
//Syntax error: Missing operand after 'by' operator.





任何帮助.....



Any help.....

推荐答案

数据表本身不会在某种程度上通过操作为您提供组合,如果你更好使用数据表上的LINQ来完成你的解决方案。



看看这个样本。





The data table itself will not provide you with group by operation some extent it will be better if you use LINQ on Data table to accomplish your solution.

Look at this Sample.


DataTable dTable = new DataTable();
        dTable.Columns.Add("id",typeof(int));
        dTable.Columns.Add("name",typeof(string));
        dTable.Rows.Add(1, "hiren");
        dTable.Rows.Add(2, "solanki");
        dTable.Rows.Add(3, "hiren");
        var query = from row in dTable.AsEnumerable()
                group row by row.Field<string>("name") into grp
                select new
                {
                    Id = grp.Key,
                    sum = grp.Sum(r=>r.Field<int>("id"))
                };
        foreach (var grp in query)
        {
            Response.Write(String.Format("The Sum of '{0}' is {1}", grp.Id, grp.sum));
        }





如果解决,请投票或标记为答案。



Please Vote or Mark as A answer if Solved.


尝试用单引号包装地址 - 分组依据+'地址+'。



在sql查询中,地址应该在单引号中 - '地址'
Try wrapping Address in single quotes - "group by " + "'" "Address" + "'".

In a sql query Adress should be in a single quote - 'Address'


你想得到一张桌子吗

Do you want to get a table with
object sum1 = dTable.Compute("SUM(AutoID)", "group by Address");





这是错误的,Compute返回一个值。



和第二个参数是过滤器

所以从mytable选择SUM(AutoId)where address ='Ram Nagar,India'

do

?

It's wrong, Compute return a single value.

and the second parameter is the filter
so for select SUM(AutoId) from mytable where address = 'Ram Nagar, India'
do

object sum1 = dTable.Compute("SUM(AutoID)", "Address = 'Ram Nagar, India'");



您可以使用所有SQL布尔命令,例如LIKE,<,>,<>


you can use all SQL boolean command such as LIKE, <, >, <>


这篇关于DataTable使用group by计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆