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

时间:2016-10-13 09:58:25

标签: 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         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

任何人都可以建议一些示例代码

由于

3 个答案:

答案 0 :(得分:3)

试试这个

PostConstruct

enter image description here

答案 1 :(得分:0)

这应该有用。

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());
        }

它按照想要的方式对它们进行排序,但显示它并不是那么简单。我目前正试图让它显示得很好

答案 2 :(得分:0)

您可以使用dynamic关键字在运行时根据您的要求创建对象。我没有为pillerId插入多个条目。在我的情况下,我在给定的pillerId的季度字段中添加了多个功能。请查看以下代码:

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();

        }
    }

}

}