var incidents = new List<SMIncident>();
while (reader.Read())
{
SMIncident smIncident = new SMIncident();
smIncident.GCSSReferenceNo = reader.GetString(0);
smIncident.assignmentGroup = reader.GetString(1);
incidents.Add(smIncident);
}
&#13;
我需要根据特定条件从List中删除重复项。 例如,
xxxx t1
yyyy t2
zzzz t1
xxxx t4
xxxx t2
&#13;
1)如果列表包含重复项,需要检查 2)如果有重复项,则检查,如果重复项具有值t4,则3)如果t4在那里则忽略所有。 基本上,我需要的是一个包含yyyy的列表 - &gt; t2,zzzz - &gt; t1这些值。
第二种情况:
xxxx t1
yyyy t2
zzzz t1
xxxx t2
&#13;
1)如果列表包含重复项,需要检查 2)如果有重复项,那么检查,如果重复项有值t4,3)如果没有t4,那么它应该具有列表中的所有值 基本上,我需要的是一个包含xxxx的列表 - &gt; t1,yyyy - &gt; t2,zzzz - &gt; t1,xxxx - &gt; t2这些值。
答案 0 :(得分:2)
根据您的抽象尝试此示例:
class AbstractList { public string field1; public string field2; }
List<AbstractList> lst = new List<AbstractList> {
new AbstractList() { field1 = "xxxx", field2="t8"},
new AbstractList() { field1 = "xxxx", field2="t1"},
new AbstractList() { field1 = "xxxx", field2="t5"},
new AbstractList() { field1 = "yyyy", field2="t1"},
new AbstractList() { field1 = "yyyy", field2="t3"},
new AbstractList() { field1 = "zzzz", field2="t1"},
new AbstractList() { field1 = "zzzz", field2="t4"},
new AbstractList() { field1 = "xxxx", field2="t5"}
};
var res = from lstElement in lst
where
(
//following linq sub-expression get an exclusion sequence for your purpose
from el_lstGrouped in
(
from i in lst
group i by new { i.field1 } into lstGrouped
where lstGrouped.Count() > 1
select lstGrouped
).SelectMany(g => g)
where el_lstGrouped.field2 == "t4"
select new
{
Field1 = el_lstGrouped.field1,
Field2 = el_lstGrouped.field2
}
).All(excluded => excluded.Field1 != lstElement.field1)
select lstElement;
foreach(var item in res)
Console.WriteLine($"{item.field1} - {item.field2}");
尝试片段:http://volatileread.com/utilitylibrary/snippetcompiler?id=99262