C# 在c中使用LINQ将行值转换为列#

C# 在c中使用LINQ将行值转换为列#,c#,linq,C#,Linq,我有一个清单如下 PillarId Quarter Feature 1 Q12106 France 1 Q12016 Germany 1 Q22016 Italy 1 Q32016 Russia 2 Q22016 India 2 Q32016 USA 3 Q22016 China 3 Q32016 Australia 3

我有一个清单如下

PillarId  Quarter  Feature
1         Q12106   France
1         Q12016   Germany 
1         Q22016   Italy
1         Q32016   Russia
2         Q22016   India
2         Q32016   USA
3         Q22016   China
3         Q32016   Australia
3         Q32016   New Zeland
3         Q42016   Japan
我想把它转换成这样的列表

pillarId  Q12016 Q22016 Q32016       Q42016
1         France Italy  Russia
1         Germany 
2                India   USA
3                China   Australia    Japan
3                        New Zeland
有人能推荐一些示例代码吗

谢谢

这应该行得通

public struct temp
        {
            public int PillarID;
            public string Quarter;
            public string Feature;
        }
        static void Main(string[] args)
        {

        List<temp> list = new List<temp>
        {
            new temp {PillarID = 1, Quarter= "Q12106", Feature = "France"},
            new temp {PillarID = 1, Quarter= "Q12106", Feature = "Germany"},
            new temp {PillarID = 1, Quarter= "Q22016", Feature = "Italy"},
            new temp {PillarID = 1, Quarter= "Q32016", Feature = "Russia"},
            new temp {PillarID = 2, Quarter= "Q22016", Feature = "India"},
            new temp {PillarID = 2, Quarter= "Q32016", Feature = "USA"},
            new temp {PillarID = 3, Quarter= "Q22016", Feature = "China"},
            new temp {PillarID = 3, Quarter= "Q32016", Feature = "Australia"},
            new temp {PillarID = 3, Quarter= "Q32016", Feature = "New Zeland"},
            new temp {PillarID = 3, Quarter= "Q42016", Feature = "Japan"}

        };
            IEnumerable<IGrouping<string, temp>> byQuarter = list.GroupBy(x => x.Quarter);
            Console.WriteLine(byQuarter.ToString());
        }
公共结构温度
{
公共卫生服务;
公共字符串季度;
公共字符串特征;
}
静态void Main(字符串[]参数)
{
列表=新列表
{
新温度{pillard=1,Quarter=“Q12106”,Feature=“France”},
新温度{pillard=1,Quarter=“Q12106”,Feature=“Germany”},
新临时工{Pillard=1,Quarter=“2016年第二季度”,Feature=“意大利”},
新临时工{Pillard=1,Quarter=“2016年第三季度”,Feature=“俄罗斯”},
新临时工{Pillard=2,Quarter=“2016年第二季度”,Feature=“印度”},
新临时工{Pillard=2,Quarter=“Q2016”,Feature=“USA”},
新临时工{Pillard=3,Quarter=“2016年第二季度”,Feature=“中国”},
新临时工{Pillard=3,Quarter=“Q2016”,Feature=“Australia”},
新临时工{Pillard=3,Quarter=“Q2016”,Feature=“new Zeland”},
新临时工{Pillard=3,Quarter=“2016年第四季度”,Feature=“日本”}
};
IEnumerable by Quarter=list.GroupBy(x=>x.Quarter);
Console.WriteLine(byQuarter.ToString());
}
它确实以想要的方式对它们进行排序,但是显示它并不是那么简单。我目前正在努力使它显示得很好

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication16
{
    class Program
    {
         static void Main(string[] args)
        {
             DataTable dt = new DataTable();
             dt.Columns.Add("PillarId", typeof(int));
             dt.Columns.Add("Quarter", typeof(string));
             dt.Columns.Add("Feature", typeof(string));

             dt.Rows.Add(new object[] {1, "Q12116", "France"});
             dt.Rows.Add(new object[] {1, "Q12116", "Germany"});
             dt.Rows.Add(new object[] {1, "Q22116", "Italy"});
             dt.Rows.Add(new object[] {1, "Q32116", "Russia"});
             dt.Rows.Add(new object[] {2, "Q22116", "India"});
             dt.Rows.Add(new object[] {2, "Q32116", "USA"});
             dt.Rows.Add(new object[] {3, "Q22116", "China"});
             dt.Rows.Add(new object[] {3, "Q32116", "Austrailia"});
             dt.Rows.Add(new object[] {3, "Q32116", "New Zeland"});
             dt.Rows.Add(new object[] {3, "Q42116", "Japan"});

             string[] uniqueQuarters = dt.AsEnumerable().Select(x => x.Field<string>("Quarter")).Distinct().ToArray();

             var groups = dt.AsEnumerable()
                 .GroupBy(x => x.Field<int>("PillarId")).Select(x => x.GroupBy(y => y.Field<string>("Quarter")).Select(z => new { id = x.Key, quarter = z.Key, feature = z.Select((a,i) => new { feature = a.Field<string>("Feature"), index = i}).ToList()}).ToList()).ToList();

             DataTable pivot = new DataTable();
             pivot.Columns.Add("PillarId", typeof(int));
             foreach (string quarter in uniqueQuarters)
             {
                 pivot.Columns.Add(quarter, typeof(string));
             }

             foreach (var group in groups)
             {
                 int maxNewRows = group.Select(x => x.feature.Count()).Max();
                 for (int index = 0; index < maxNewRows; index++)
                 {
                     DataRow newRow = pivot.Rows.Add();
                     foreach (var row in group)
                     {
                         newRow["PillarId"] = row.id;
                         newRow[row.quarter] = row.feature.Skip(index) == null || !row.feature.Skip(index).Any() ? "" : row.feature.Skip(index).First().feature;
                     }
                 }
             }
        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
命名空间控制台应用程序16
{
班级计划
{
静态void Main(字符串[]参数)
{
DataTable dt=新的DataTable();
添加(“Pillard”,类型(int));
添加(“季度”,类型(字符串));
添加(“特征”,类型(字符串));
添加(新对象[]{1,“Q12116”,“France”});
添加(新对象[]{1,“Q12116”,“德国”});
Add(新对象[]{1,“Q22116”,“意大利”});
添加(新对象[]{1,“Q32116”,“俄罗斯”});
添加(新对象[]{2,“Q22116”,“India”});
Add(新对象[]{2,“Q32116”,“USA”});
添加(新对象[]{3,“Q22116”,“China”});
Add(新对象[]{3,“Q32116”,“Austrailia”});
Add(新对象[]{3,“Q32116”,“new Zeland”});
添加(新对象[]{3,“Q42116”,“Japan”});
字符串[]uniqueQuarters=dt.AsEnumerable()。选择(x=>x.Field(“Quarter”)).Distinct().ToArray();
变量组=dt.AsEnumerable()
.GroupBy(x=>x.Field(“pilland”)).Select(x=>x.GroupBy(y=>y.Field(“Quarter”)).Select(z=>new{id=x.Key,Quarter=z.Key,feature=z.Select((a,i)=>new feature=a.Field(“feature”),index=i}.ToList().ToList()).ToList();
DataTable pivot=新DataTable();
pivot.Columns.Add(“pillard”,typeof(int));
foreach(唯一四分之一中的四分之一字符串)
{
pivot.Columns.Add(四分之一,typeof(字符串));
}
foreach(组中的var组)
{
int maxNewRows=group.Select(x=>x.feature.Count()).Max();
对于(int index=0;index

您可以在运行时根据需要使用dynamic关键字创建对象。我没有为一个pillerId插入多个条目。在我的例子中,我为一个给定的pillerId在quarter字段中添加了多个特性。请查看以下代码以获取相同的信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Dynamic;

namespace Test
{
    public class Piller
    {
        public int PillarId;
        public string Quarter;
        public string Feature;

    public static List<dynamic> Generate()
    {
        List<Piller> pillers = new List<Piller>();
        pillers.Add(new Piller { PillarId = 1, Quarter = "Q12106", Feature = "France" });
        pillers.Add(new Piller { PillarId = 1, Quarter = "Q12106", Feature = "Germany" });
        pillers.Add(new Piller { PillarId = 1, Quarter = "Q22016", Feature = "Italy" });
        pillers.Add(new Piller { PillarId = 1, Quarter = "Q32016", Feature = "Russia" });
        pillers.Add(new Piller { PillarId = 2, Quarter = "Q22016", Feature = "Italy" });
        pillers.Add(new Piller { PillarId = 2, Quarter = "Q32016", Feature = "USA" });
        pillers.Add(new Piller { PillarId = 3, Quarter = "Q22016", Feature = "China" });
        pillers.Add(new Piller { PillarId = 3, Quarter = "Q32016", Feature = "Australia" });
        pillers.Add(new Piller { PillarId = 3, Quarter = "Q32016", Feature = "New Zeland" });
        pillers.Add(new Piller { PillarId = 3, Quarter = "Q42016", Feature = "Japan" });


        var pillerIds = (from p in pillers
                        select p.PillarId).Distinct();
        var quarters = (from p in pillers
                        select p.Quarter).Distinct();
        List<dynamic> transformedData = new List<dynamic>();

        foreach (var pillerId in pillerIds)
        {
            var data = new ExpandoObject() as IDictionary<string, Object>;
            data.Add("pillerId",pillerId);

            foreach (var quarter in quarters)
            {
                var features = (from p in pillers
                                where p.PillarId == pillerId && p.Quarter == quarter
                                select p.Feature);
                data.Add(quarter,features);
            }
            transformedData.Add(data);
        }
        return transformedData;

    }

    public static void Print(List<dynamic> data)
    {
        var dic = data[0] as IDictionary<string, Object>;
        foreach (var field in dic.Keys)
        {
            Console.Write(field+" ");
        }
        Console.WriteLine();
        foreach (dynamic record in data)
        {
            dic = record as IDictionary<string, Object>;
            foreach (var field in dic.Keys)
            {
                if (field == "pillerId")
                    Console.Write(dic[field] + " ");
                else
                {
                    var value = dic[field];
                    if (value == null)
                    {
                        Console.Write("      ");
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (var item in (value as IEnumerable<string>))
                        {
                            if (sb.Length == 0)
                                sb.Append(item);
                            else
                                sb.Append(","+item);
                        }
                        Console.Write(sb.ToString());
                    }
                }
                Console.Write(" ");
            }
            Console.WriteLine();

        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
运用系统动力学;
名称空间测试
{
公营起球机
{
公共卫生服务;
公共字符串季度;
公共字符串特征;
公共静态列表生成()
{
列表pillers=新列表();
添加(新的Piller{pillard=1,Quarter=“Q12106”,Feature=“France”});
添加(新的Piller{pillard=1,Quarter=“Q12106”,Feature=“Germany”});
添加(新Piller{Pillard=1,Quarter=“2016年第二季度”,Feature=“意大利”});
pillers.Add(新Piller{Pillard=1,Quarter=“2016年第三季度”,Feature=“俄罗斯”});
pillers.Add(新Piller{Pillard=2,Quarter=“2016年第二季度”,Feature=“意大利”});
添加(新Piller{Pillard=2,Quarter=“2016年第三季度”,Feature=“USA”});
pillers.Add(新Piller{Pillard=3,Quarter=“2016年第二季度”,Feature=“中国”});
添加(新Piller{Pillard=3,Quarter=“2016年第三季度”,Feature=“澳大利亚”});
添加(new Piller{pillard=3,Quarter=“2016年第三季度”,Feature=“new Zeland”});
添加(新Piller{Pillard=3,Quarter=“Q42016”,Feature=“Japan”});
var pillerIds=(来自pillers中的p
选择p.pillard).Distinct();
var Quarter=(从皮勒中的p开始)
选择p.Quarter).Distinct();
List transformedData=新列表();
foreach(pillerIds中的变量pillerId)
{
var data=新的ExpandooObject()作为IDictionary;
添加(“pillerId”,pillerId);
foreach(var季度对季度)
{
变量特征=(来自pillers中的p
其中p.Pillard==pillerId和p.Quarter==夸脱