LINQ使用C#来交换一些列到数据表的行 [英] LINQ to Swap few Columns to Rows of a DataTable using C#

查看:160
本文介绍了LINQ使用C#来交换一些列到数据表的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据表:

 位置季度ppl_required ppl_available
BLR Q1 70 35
BLR Q2 50 45
BLR Q3 25 28
BLR Q4 60 58
CHN Q1 77 92
CHN Q2 42 66
CHN Q3 29 20
CHN Q4 22 24
 

有没有更好的办法让下面的数据表作为输出非常简单或短期的方法[没有循环]使用 LINQ 或先进功能 LINQ 无论是与.NET3.5 / 4.0 / 4.5框架。

 位置ppl_Required_Q1 ppl_Required_Q2 ppl_Required_Q3 ppl_Required_Q4 ppl_available_Q1 ppl_available_Q2 ppl_available_Q3 ppl_available_Q4
BLR 70 50 25 60 35 45 28 58
CHN 77 42 29 22 92 66 20 24
 

解决方案

我不知道你已经试过,如果有任何的效率和灵活性的需求,或者是你真的需要一个输出容器,但也许事情就这么简单,这是很有用的。假设 DT 是数据表:

  VAR newSet = dt.AsEnumerable()
               .GroupBy(R => r.Field<字符串>(位置))
               。选择(G =>新建
               {
                    位置= g.Key,
                    ppl_required_Q1 = g.Where(P => p.Field<字符串>(季)==Q1)总和。(P => p.Field< INT>(ppl_required)),
                    ppl_required_Q2 = g.Where(P => p.Field<字符串>(季)==Q2)总和。(P => p.Field< INT>(ppl_required)),
                    ppl_required_Q3 = g.Where(P => p.Field<字符串>(季)==Q3)总和。(P => p.Field< INT>(ppl_required)),
                    ppl_required_Q4 = g.Where(P => p.Field<字符串>(季)==Q4)总和。(P => p.Field< INT>(ppl_required)),
                    ppl_available_Q1 = g.Where(P => p.Field<字符串>(季)==Q1)总和。(P => p.Field< INT>(ppl_available)),
                    ppl_available_Q2 = g.Where(P => p.Field<字符串>(季)==Q2)总和。(P => p.Field< INT>(ppl_available)),
                    ppl_available_Q3 = g.Where(P => p.Field<字符串>(季)==Q3)总和。(P => p.Field< INT>(ppl_available)),
                    ppl_available_Q4 = g.Where(P => p.Field<字符串>(季)==Q4)总和。(P => p.Field< INT>(ppl_available)),
                });
 

修改

添加从实例这里和组装的扩展方法< A HREF =htt​​p://www.extensionmethod.net/Details.aspx?ID=249相对=nofollow>此处的情况下链接在未来得到打破。您应该能够修改此需要。

 公共静态数据表ToDataTable&LT; T&GT;(这IEnumerable的&LT; T&GT;源,串newTableName)
{
    数据表newtable的=新的DataTable(newTableName);

    牛逼FIRSTROW = source.FirstOrDefault();
    如果(FIRSTROW!= NULL)
    {
        的PropertyInfo []属性= firstRow.GetType()GetProperties中()。
        的foreach(的PropertyInfo道具的属性)
        {
            newTable.Columns.Add(prop.Name,prop.PropertyType);
        }

        的foreach(源牛逼元)
        {
            DataRow的NEWROW = newTable.NewRow();
            的foreach(的PropertyInfo道具的属性)
            {
                NEWROW [prop.Name] = prop.GetValue(元素,空);
            }
            newTable.Rows.Add(NEWROW);
        }
    }
    返回newtable的;
}
 

I've datatable:

location    Quarter   ppl_required   ppl_available
BLR          Q1        70             35
BLR          Q2        50             45
BLR          Q3        25             28
BLR          Q4        60             58
CHN          Q1        77             92
CHN          Q2        42             66
CHN          Q3        29             20
CHN          Q4        22             24

Is there a better way to get the below DataTable as output in very simple or short way [without loops] using LINQ or with advanced features of LINQ either with .NET3.5/4.0/4.5 framework.

Location  ppl_Required_Q1  ppl_Required_Q2  ppl_Required_Q3  ppl_Required_Q4  ppl_available_Q1  ppl_available_Q2  ppl_available_Q3  ppl_available_Q4
BLR       70               50               25               60               35                45                28                58
CHN       77               42               29               22               92                66                20                24

解决方案

I'm not sure what you've tried, if there are any efficiency or flexibility needs, or what you really need for an output container, but perhaps something as simple as this is useful. Assuming dt is your datatable:

var newSet = dt.AsEnumerable()
               .GroupBy(r => r.Field<string>("Location"))
               .Select(g => new
               {
                    Location = g.Key,
                    ppl_required_Q1 = g.Where(p => p.Field<string>("Quarter") == "Q1").Sum(p => p.Field<int>("ppl_required")),
                    ppl_required_Q2 = g.Where(p => p.Field<string>("Quarter") == "Q2").Sum(p => p.Field<int>("ppl_required")),
                    ppl_required_Q3 = g.Where(p => p.Field<string>("Quarter") == "Q3").Sum(p => p.Field<int>("ppl_required")),
                    ppl_required_Q4 = g.Where(p => p.Field<string>("Quarter") == "Q4").Sum(p => p.Field<int>("ppl_required")),
                    ppl_available_Q1 = g.Where(p => p.Field<string>("Quarter") == "Q1").Sum(p => p.Field<int>("ppl_available")),
                    ppl_available_Q2 = g.Where(p => p.Field<string>("Quarter") == "Q2").Sum(p => p.Field<int>("ppl_available")),
                    ppl_available_Q3 = g.Where(p => p.Field<string>("Quarter") == "Q3").Sum(p => p.Field<int>("ppl_available")),
                    ppl_available_Q4 = g.Where(p => p.Field<string>("Quarter") == "Q4").Sum(p => p.Field<int>("ppl_available")),
                });

EDIT

Adding an extension method assembled from examples here and here in case links get broken in the future. You should be able to modify this as needed.

public static DataTable ToDataTable<T>(this IEnumerable<T> source, string newTableName)
{
    DataTable newTable = new DataTable(newTableName);

    T firstRow = source.FirstOrDefault();
    if (firstRow != null)
    {
        PropertyInfo[] properties = firstRow.GetType().GetProperties();
        foreach (PropertyInfo prop in properties)
        {
            newTable.Columns.Add(prop.Name, prop.PropertyType);
        }

        foreach (T element in source)
        {
            DataRow newRow = newTable.NewRow();
            foreach (PropertyInfo prop in properties)
            {
                newRow[prop.Name] = prop.GetValue(element, null);
            }
            newTable.Rows.Add(newRow);
        }
    }
    return newTable;
}

这篇关于LINQ使用C#来交换一些列到数据表的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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