C# C中ListView的问题#
因此,我正在学习C# C中ListView的问题#,c#,linq,C#,Linq,因此,我正在学习LINQ,并被分配处理两个.txt文件并加入它们 到目前为止,我做得很好,但我在展示方面遇到了一些僵局。我应该让名称显示一次,然后下面关闭的案例只有案例信息 我遇到的问题是,在列表视图中列出数据集后,名称会不断重复。我认为LINQ语句或我在foreach循环中的方式有问题。以下是主要表格的代码: //Fills the lists by calling the methods from the DB classes techs = TechnicianDB
LINQ
,并被分配处理两个.txt
文件并加入它们
到目前为止,我做得很好,但我在展示方面遇到了一些僵局。我应该让名称显示一次,然后下面关闭的案例只有案例信息
我遇到的问题是,在列表视图中列出数据集后,名称会不断重复。我认为LINQ
语句或我在foreach
循环中的方式有问题。以下是主要表格的代码:
//Fills the lists by calling the methods from the DB classes
techs = TechnicianDB.GetTechnicians();
incidents = IncidentDB.GetIncidents();
//Creates a variable to use in the LINQ statements
var ClosedCases = from Incident in incidents
join Technician in techs
on Incident.TechID equals Technician.TechID
where Incident.DateClosed!= null
orderby Technician.Name, Incident.DateOpened descending
select new { Technician.Name, Incident.ProductCode, Incident.DateOpened, Incident.DateClosed, Incident.Title };
//variables to hold the technician name, and the integer to increment the listview
string techName = "";
int i = 0;
//foreach loop to pull the fields out of the lists and to display them in the required areas in the listview box
foreach (var Incident in ClosedCases)
{
foreach (var Technician in ClosedCases)
{
if (Technician.Name != techName)
{
lvClosedCases.Items.Add(Technician.Name);
techName = Technician.Name;
}
else
{
lvClosedCases.Items.Add("");
}
}
lvClosedCases.Items[i].SubItems.Add(Incident.ProductCode);
lvClosedCases.Items[i].SubItems.Add(Incident.DateOpened.ToString());
lvClosedCases.Items[i].SubItems.Add(Incident.DateClosed.ToString());
lvClosedCases.Items[i].SubItems.Add(Incident.Title);
i++;
}
下面是我得到的结果:
从右侧的栏中可以看出,列表将继续显示多个列
我错过了什么
多谢各位
编辑:对于每个请求,下面是结果应该是什么样子:
为什么要重复两次已结束的案例
foreach (var Incident in ClosedCases)
{
foreach (var Technician in ClosedCases)
{
}
}
提供一个示例,您的预期输出和实际输出将澄清您的问题。因为没有人知道你的原始数据,所以很难提供帮助。正如路易斯提到的,请提供一个例子。很难想象您期望什么和收到什么。这不是问题的直接答案,但您是否考虑过使用ObjectListView控件?这需要一些时间来适应,但是这样的事情更容易处理。嗨,Matthew,欢迎来到StackOverflow。我认为您缺少的是逐步完成代码并对其进行调试的能力。如果这样做,您将看到嵌套的foreach
语句导致了问题。我建议您学习如何在使用的任何IDE中逐步执行代码。