linq - 使用 LINQ 的动态枢轴是否可能?

标签 linq tsql dynamic pivot

我有一个返回的 T-SQL 2005 查询:

pid         propertyid  displayname     value
----------- ----------- --------------- ---------------
14270790    74          Low Price       1.3614
14270790    75          High Price      0
14270791    74          Low Price       1.3525
14270791    75          High Price      0
14270792    74          Low Price       1.353
14270792    75          High Price      0
14270793    74          Low Price       1.3625
14270793    75          High Price      0
14270794    74          Low Price       1.3524
14270794    75          High Price      0

我想做的基本上是在 displayname 字段上,希望产生:

pid       Low Price  High Price
14270790  1.3614     0
14270791  1.3525     0
14270792  1.353      0
14270793  1.3625     0
14270794  1.3524     0

(不确定 propertyid 字段将如何输出,所以我将其省略(希望它能简单地与低价和高价字段并排显示它们的 ID,但我认为这行不通。 )

问题在于原始 displayname 字段的内容是动态的 - 它是通过与 PropertyName' 表的连接生成的,因此旋转列的数量是可变的。因此,它可能包含High PriceLow PriceOpenClose`,具体取决于与该表的连接返回的内容。

当然,在固定查询或存储过程中生成此枢轴相对容易(不管我在编写初始查询时遇到的麻烦!)。但是,是否可以让 LINQ 生成一个 SQL 查询来命名要生成的每个列,而不必编写一个列出列名称的动态(可能在存储过程中)查询?

谢谢,

马特。

最佳答案

我会给你一个包含不同数据(我需要的)的样本。您可以根据需要进行调整。请注意,只使用了两个 linq 查询,其他大部分内容是将列表转换为数据表。

         var data = new[] {
                new{Student=1, Subject="English", Marks=40},
                new{Student=1, Subject="Maths", Marks=50},
                new{Student=1, Subject="Science", Marks=60},
                new{Student=1, Subject="Physics", Marks=70},
                new{Student=1, Subject="Chemistry", Marks=80},
                new{Student=1, Subject="Biology", Marks=90},
                new{Student=2, Subject="English", Marks=4},
                new{Student=2, Subject="Maths", Marks=5},
                new{Student=2, Subject="Science", Marks=6},
                new{Student=2, Subject="Physics", Marks=7},
                new{Student=2, Subject="Chemistry", Marks=8},
                new{Student=2, Subject="Biology", Marks=9}
            };

        /*Here the pivot column is the subject and the static column is student
        group the data against the static column(s)*/

        var groups = from d in data
                     group d by d.Student into grp
                     select new
                     {
                         StudentId = grp.Key,
                         Marks = grp.Select(d2 => new { d2.Subject, d2.Marks }).ToArray()
                     };


        /*get all possible subjects into a separate group*/
        var subjects = (from d in data
                        select d.Subject).Distinct();

        DataTable dt = new DataTable();

        /*for static cols*/
        dt.Columns.Add("STUDENT_ID");


        /*for dynamic cols*/
        foreach (var subject in subjects)
        {
            dt.Columns.Add(subject.ToString());
        }

        /*pivot the data into a new datatable*/
        foreach (var g in groups)
        {
            DataRow dr = dt.NewRow();
            dr["STUDENT_ID"] = g.StudentId;

            foreach (var mark in g.Marks)
            {
                dr[mark.Subject] = mark.Marks;
            }
            dt.Rows.Add(dr);
        }   

关于linq - 使用 LINQ 的动态枢轴是否可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3565168/

相关文章:

c# - 将带有子查询的sql查询转换为linq语句

c# - 如何使用 orderbydescending 在 linq 中查询?

sql - 从数据库中的每个表中选择每一行,其中 (columnName = value) 如果列名存在

sql-server - 日期转换和文化 : Difference between DATE and DATETIME

c# - 使用 Linq 的 Where 子句 - 搜索对象列表

c# - 如何使用 LINQ 在 C# 中连接两个 List<string>

sql - 如何在 SQL 中将 Float 转换为 Varchar

c - 是否可以在编译期间动态创建等效的limits.h宏?

C 动态数组的静态指针

c - 未显示 c 中结构数组的正确值