我有两个名为tableFileTemp2
和tableFile
的表。我需要一个明确的计算机列表,将path
和file
列合并到tableFile
表path
列...
tableFileTemp2
//来源
+-------+----------+--------+-------+
| host | path | date | file |
+-------+----------+--------+-------+
| comp1 | c:\ | xydate | x.exe |
| comp1 | c:\Temp\ | xydate | x.exe |
| comp2 | c:\win\ | xydate | y.exe |
| comp2 | c:\win\ | xydate | z.exe |
+-------+----------+--------+-------+
tableFile
//这是结果,显然每台机器都复制了第一个路径+文件
+-------+---------------------------------------------------+--------+------+
| host | path | date | file |
+-------+---------------------------------------------------+--------+------+
| comp1 | c:\x.exe<br>c:\x.exe<br>c:\Temp\x.exe<br> | xydate | null |
| comp2 | c:\win\y.exe<br>c:\win\y.exe<br>c:\win\z.exe<br> | xydate | null |
+-------+---------------------------------------------------+--------+------+
tableFile
//预期结果
+-------+----------------------------------+--------+------+
| host | path | date | file |
+-------+----------------------------------+--------+------+
| comp1 | c:\x.exe<br>c:\Temp\x.exe<br> | xydate | null |
| comp2 | c:\win\y.exe<br>c:\win\z.exe<br> | xydate | null |
+-------+----------------------------------+--------+------+
我的代码:
for (int s = 0; s < tableFileTemp2.Rows.Count; s++)
{
if (tableFile.Rows.Count != 0)
{
for (int t = 0; t < tableFile.Rows.Count; t++)
{
if (string.Equals(tableFile.Rows[t][0].ToString(), tableFileTemp2.Rows[s][0].ToString(), StringComparison.OrdinalIgnoreCase))
{
tableFile.Rows[t][1] = tableFile.Rows[t][1].ToString() + tableFileTemp2.Rows[s][1].ToString() + tableFileTemp2.Rows[s][3].ToString() + "<br>";
break;
}
else if (t == (tableFile.Rows.Count - 1))
{
tableFile.Rows.Add(tableFileTemp2.Rows[s][0].ToString(), (tableFileTemp2.Rows[s][1].ToString() + tableFileTemp2.Rows[s][3].ToString() + "<br>"), tableFileTemp2.Rows[s][2], null);
}
}
}
else
{
tableFile.Rows.Add(tableFileTemp2.Rows[s][0].ToString(), (tableFileTemp2.Rows[s][1].ToString() + tableFileTemp2.Rows[s][3].ToString() + "<br>"), tableFileTemp2.Rows[s][2], null);
}
}
答案 0 :(得分:2)
正如您在评论中所说,您仍然拥有数据来自的原始CSV。我们可以用LINQ做到这一点。
+-------+----------+--------+-------+
| host | path | date | file |
+-------+----------+--------+-------+
| comp1 | c:\ | xydate | x.exe |
| comp1 | c:\Temp\ | xydate | x.exe |
| comp2 | c:\win\ | xydate | y.exe |
| comp2 | c:\win\ | xydate | z.exe |
+-------+----------+--------+-------+
然后代码看起来像。
var results =
from thing in
(from line in tableFileCSV.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None)
let row = line.Split(',')
select new
{
Host = row[0],
Path = row[1] + row[3] + "</br>",
Date = row[2],
File = row[3] // <- Are you sure you want this to be null and not the file value?
})
group new { thing.Path, thing.Date, thing.File } by new { thing.Host } into g
select new
{
Host = g.Key.Host,
Path = g.Select(i => i.Path).Aggregate((a, b) => a + b),
Date = g.Select(i => i.Date).FirstOrDefault(),
File = "File",
};
// If you want to get a look at it.
foreach (var item in results)
{
Console.WriteLine("{0} {1} {2} {3}", item.Host, item.Path, item.Date, item.File);
}
tableFileCSV
是CSV中的原始数据。
然后可以将其传递到数据表中。
var dt = new DataTable();
dt.Columns.Add("Host");
dt.Columns.Add("Path");
dt.Columns.Add("Date");
dt.Columns.Add("File");
foreach (var item in results)
{
dt.Rows.Add(item.Host,item.Path,item.Date,item.File);
}