是否可以使用LINQ创建动态轴?

是否可以使用LINQ创建动态轴?,linq,tsql,dynamic,pivot,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

我有一个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'表的联接生成的,因此数据透视列的数量是可变的。因此,它可能包含
高价
低价
打开
关闭',具体取决于与该ta的联接不可预测的回报

当然,在固定查询或存储过程中生成此透视相对容易(不管编写初始查询有多麻烦!)。但是,是否可以让LINQ生成一个SQL查询,该查询将命名要生成的每个列,而不必编写动态(可能在存储过程中)呢查询哪些列列出了列名

谢谢


马特。

这是我能得到的最接近的,但不是林克

create table #t
(
    pointid [int],
    doublevalue [float],
    title [nvarchar](50)
)

insert into #t
    select
        distinct top 100
        v.pointid, v.doublevalue, p.title
    from [property] p
        inner join pointvalue v on p.propertyid = v.propertyid
        inner join point pt on v.pointid = pt.pointid
    where v.pointid in (select top 5 p.pointid from point p where p.instanceid = 36132)

declare @fields nvarchar(250)
set @fields = (select STUFF((SELECT N',[' + title + ']' FROM [property] FOR XML PATH('')), 1, 1, N''))
--select @fields

declare @sql nvarchar(500)
set @sql = 'select * from #t
pivot
(
    sum(doublevalue)
    for [title] in ('+@fields+')
) as alias'
--select @sql

exec (@sql)

drop table #t

关键是我只是要求属性表中的每个条目,这意味着在结果透视中有许多列具有空值。

我将给您一个包含不同数据的示例(我需要)。您可以根据自己的需要进行调整。注意,只使用了两个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);
        }   

我认为代码是这样的:

var list = from table in Property
                       group table by table.pid into g
                       select new
                       {
                           pid = g.key,
                           LowPrice = g.Where(w => w.pid== g.key && w.priceType == "low").Select(s => s.value).FirstorDefault(),
                           HighPrice = g.Where(w => w.pid== g.key && w.priceType == "high").Select(s => s.value).FirstorDefault(),
                       };

希望它能帮助您,祝您度过愉快的一天。

这可以在
IList
IQueryable
中返回吗?