C#linq枢轴还是交叉表或转置?
有史以来的第一篇帖子,请原谅,如果我的话C#linq枢轴还是交叉表或转置?,c#,linq,C#,Linq,有史以来的第一篇帖子,请原谅,如果我的话 StreamWriter noteswriter = new StreamWriter(directoryPath + "\\" + "Notes.CSV"); var NotesQuery = from w in (from line in BillSummaryQuery1 let linerecord2 = line.Split(',').ToList() where linerecor
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
如有任何建议,将不胜感激。谢谢 试试这个:
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());
如果
colA=AccountA,colB=Number1
出现在列表的更下方,那么会将第一行扩展到Address2
之外,还是会开始一个新行?这就是我要找的。这只会延长第一条线。非常感谢!非常感谢。成功了!我正在学习C#和LINQ。你有什么有用的提示、建议或资源吗?@SaidurRahman-如果你还没有使用它,我会得到“LINQPad”。测试LINQ代码非常棒。值得为自动完成付费。另外,你应该投票接受我的回答它说投票需要15%的声誉。我只有一个。谢谢,我会跟着你的!“这是我的第一个问题,我真的很高兴我在这里。”赛杜拉曼-不用担心。欢迎登机。你不需要很长时间就能得到名声。开始回答问题。@SaidurRahman-简单的错误。Take(max)
应该是。Take(max+2)
。我修正了答案。