C#linq pivot或交叉表或转置?

时间:2011-10-06 08:36:20

标签: c# linq

如果我不在线,请原谅第一篇文章。

StreamWriter noteswriter = new StreamWriter(directoryPath + "\\" + "Notes.CSV");

var NotesQuery =
    from w in
        (from line in BillSummaryQuery1
         let linerecord2 = line.Split(',').ToList()
         where linerecord2[0] == "RB" && linerecord2[1] == "MICA" && linerecord2[6].Trim() == "1" && linerecord2[7].Trim() == "H" && linerecord2[9].Trim() == "D" && Regex.IsMatch(linerecord2[10].Substring(4, 1), @"^[a-zA-Z0-9]*$") && linerecord2[10].Substring(2, 1) == " "
         select string.Concat(linerecord2[2], ",", linerecord2[3], ",", linerecord2[10].Trim())
         )
    let lineorder3 = w.Split(',').ToList()
    select new
    {
        colA = lineorder3[0],
        colB = lineorder3[1],
        colC = lineorder3[2]
    };

var NotesQuery1 =
from w in NotesQuery
    group w by new { w.colA, w.colB, w.colC } into g
    select new
    {
        colA = g.Key.colA, 
        colB = g.Key.colB, 
        colC = g.Key.colC
    };

foreach (var item in NotesQuery1)
{
    noteswriter.WriteLine(item);
    toolStripProgressBar1.ProgressBar.PerformStep();
}

noteswriter.Dispose();
noteswriter.Close();

这给了我一个输出,如:

{colA = AccountA,colB = Number1,colC = AccountA }

{colA = AccountA,colB = Number1,colC = AccountHolderA }

{colA = AccountA,colB = Number1,colC = Address1 }

{colA = AccountA,colB = Number1,colC = Address2 }

{colA = AccountB,colB = Number2,colC = AccountA }

{colA = AccountB,colB = Number2,colC = AccountHolderB }

{colA = AccountB,colB = Number2,colC = Address1 }

我不知道如何将CSV文件写成:

AccountA,Number1,AccountA,AccountHolderA,Address1,Address2

AccountB,Number2,AccountA,AccountHolderB,Address

任何建议都将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:2)

试试这个:

var NotesQuery =
    from line in BillSummaryQuery1
    let linerecord2 = line.Split(',')
    where linerecord2[0] == "RB"
    where linerecord2[1] == "MICA"
    where linerecord2[6].Trim() == "1"
    where linerecord2[7].Trim() == "H"
    where linerecord2[9].Trim() == "D"
    where Regex.IsMatch(linerecord2[10].Substring(4, 1), @"^[a-zA-Z0-9]*$")
    where linerecord2[10].Substring(2, 1) == " "
    group linerecord2[10].Trim() by new
    {
        A = linerecord2[2],
        B = linerecord2[3],
    } into g
    select String.Join(",", (new []
    {
        g.Key.A,
        g.Key.B,
    }).Concat(g.Distinct()).ToArray());

foreach (var item in NotesQuery)
{
    noteswriter.WriteLine(item);
    toolStripProgressBar1.ProgressBar.PerformStep();
}

编辑:以下是如何使列数相等。

var NotesQuery =
    (from line in BillSummaryQuery1
    let linerecord2 = line.Split(',')
    where linerecord2[0] == "RB"
    where linerecord2[1] == "MICA"
    where linerecord2[6].Trim() == "1"
    where linerecord2[7].Trim() == "H"
    where linerecord2[9].Trim() == "D"
    where Regex.IsMatch(linerecord2[10].Substring(4, 1), @"^[a-zA-Z0-9]*$")
    where linerecord2[10].Substring(2, 1) == " "
    group linerecord2[10].Trim() by new
    {
        A = linerecord2[2],
        B = linerecord2[3],
    } into g
    select new
    {
        g.Key.A,
        g.Key.B,
        Cs = g.Distinct().ToArray()
    }).ToArray();

var max = NotesQuery.Max(nq => nq.Cs.Length);

var padding = from n in Enumerable.Range(0, max) select "";

var NotesQuery2 =
    from nq in NotesQuery
    select String.Join(",", (new []
    {
        nq.A,
        nq.B,
    }).Concat(nq.Cs).Concat(padding).Take(max + 2).ToArray());