Linq交叉表和分组

时间:2013-02-22 10:11:05

标签: c# asp.net linq entity-framework pivot

我有一组看起来像这样的数据:

ItemID | ProcessID | Sequence
Item 1 | ProcessA  | 1 
Item 1 | ProcessB  | 2 
Item 3 | ProcessA  | 1 
Item 2 | ProcessA  | 1 
Item 1 | ProcessC  | 3
Item 3 | ProcessB  | 2

我想创建一个linq查询来格式化这样的数据:

ItemID | Sequence 1 | Sequence 2 | Sequence 3 | Sequence N 
Item 1 | ProcessA   | ProcessB   | ProcessC 
Item 2 | ProcessA   |  
Item 3 | ProcessA   | ProcessB

有一种简单的方法吗?

2 个答案:

答案 0 :(得分:1)

我假设了避免动态列的进程数。不理想,但它适用于我的应用程序。

public static IQueryable GetProcessesForAllItems()
{
    var ctx = new CS3Entities();

    var allprocesses = (from pi in ctx.ProcessesItems
                        orderby pi.SequenceNumber descending
                        select new
                                   {
                                       pi.ItemID,
                                       pi.ProcessID,
                                       pi.SequenceNumber
                                   });

    var query = allprocesses
        .GroupBy(c => c.ItemID)
        .Select(g => new
                         {
                             ItemId = g.Key,
                             Seq100 = g.FirstOrDefault(c => c.SequenceNumber == 100).ProcessID,
                             Seq200 = g.FirstOrDefault(c => c.SequenceNumber == 200).ProcessID,
                             Seq300 = g.FirstOrDefault(c => c.SequenceNumber == 300).ProcessID,
                             Seq400 = g.FirstOrDefault(c => c.SequenceNumber == 400).ProcessID,
                             Seq500 = g.FirstOrDefault(c => c.SequenceNumber == 500).ProcessID,
                             Seq600 = g.FirstOrDefault(c => c.SequenceNumber == 600).ProcessID,
                             Seq700 = g.FirstOrDefault(c => c.SequenceNumber == 700).ProcessID,
                             Seq800 = g.FirstOrDefault(c => c.SequenceNumber == 800).ProcessID,
                             Seq900 = g.FirstOrDefault(c => c.SequenceNumber == 900).ProcessID,
                             //etc
                         });
    return query;

}

答案 1 :(得分:1)

我会使用纯sql方式来解决这个问题。所以这是给你的建议:

测试数据

CREATE TABLE #T
(
    ItemID VARCHAR(100),
    ProcessID VARCHAR(100), 
    Sequence INT
)
INSERT INTO #T
VALUES
    ('Item 1','ProcessA',1), 
    ('Item 1','ProcessB',2),
    ('Item 3','ProcessA',1),
    ('Item 2','ProcessA',1),
    ('Item 1','ProcessC',3),
    ('Item 3','ProcessB',2)

支点的列

DECLARE @cols VARCHAR(MAX)
;WITH CTE
AS
(
SELECT
    ROW_NUMBER() OVER(PARTITION BY Sequence ORDER BY Sequence) AS RowNbr,
    'Sequence '+CAST(t.Sequence AS VARCHAR(10)) AS Sequence
FROM
    #T AS t
)
SELECT @cols=STUFF
(
    (
        SELECT
            ',' +QUOTENAME([Sequence])
        FROM
            CTE
        WHERE CTE.RowNbr=1
        FOR XML PATH('')
    )
,1,1,'')

执行动态数据透视

DECLARE @query NVARCHAR(4000)=
N'SELECT
    *
FROM
(
    SELECT
        t.ItemID,
        t.ProcessID,
        ''Sequence ''+CAST(t.Sequence AS VARCHAR(10)) AS Sequence
    FROM
        #T AS t
) AS SourceTable
PIVOT
(
    MAX(ProcessID)
    FOR Sequence IN ('+@cols+')
) AS p'

EXECUTE(@query)

删除临时表

DROP TABLE #T