如何从IList中删除重复值,c#

如何从IList中删除重复值,c#,c#,.net,C#,.net,我有一个ilist,有6条记录,类似这样 第一排 时间 11:00 地点: 班加罗尔 第二排 时间 11:00 位置 空的 我必须取消第二排 同时具有空位置(11:00) 像这样,我将有数千条记录,我需要从中删除这些记录 有什么解决方案吗?您不妨简单地执行一个“for”语句,并删除不符合条件的元素(每次执行删除时,请小心使用step变量“不递增”)公共列表GetRowsWhothoutDuplicates(列表源代码) { List filteredRows=新列表(源); foreach(源

我有一个ilist,有6条记录,类似这样

第一排

时间 11:00

地点:

班加罗尔

第二排 时间 11:00

位置 空的

我必须取消第二排 同时具有空位置(11:00)

像这样,我将有数千条记录,我需要从中删除这些记录


有什么解决方案吗?

您不妨简单地执行一个“for”语句,并删除不符合条件的元素(每次执行删除时,请小心使用step变量“不递增”)

公共列表GetRowsWhothoutDuplicates(列表源代码)
{
List filteredRows=新列表(源);
foreach(源中的行)
{
如果(!filteredRows.Exists(r=>r.Time==row.Time))
{
filteredRows.Add(行);
}
}
返回
滤池;
}

您可以执行以下操作:

list.GroupBy(x=>x.Time)
    //from the grouped items select the first one that doesn't have an empty location - you'll have null here if none is found
    .Select(x=>x.FirstOrDefault(y=>!string.IsNullOrEmpty(y.Location)))
    .ToList()

总是相邻的行吗?>您是否希望仅消除具有空位置和重复时间的记录?如果位置为空,但时间不重复怎么办?位置不为空但时间重复或位置不同的记录又如何?你能完成这句话吗:删除所有时间重复的记录……如果空位置的重复行出现在要保留的行之前会怎样?+1,但OP仍然需要确认如果一个记录有不同的(但不是空)值(如果有)。你是对的,但我希望这能为他指明一个好的方向。我认为,从这一点上,他应该能够想出一个适合自己情况的解决方案
list.GroupBy(x=>x.Time)
    //from the grouped items select the first one that doesn't have an empty location - you'll have null here if none is found
    .Select(x=>x.FirstOrDefault(y=>!string.IsNullOrEmpty(y.Location)))
    .ToList()