我想用一些重复的文本和/没有id值来查询一个列表。
我有两个条件要匹配,
类型文本Id名称约翰名字约翰22名约翰史密斯2548霍比唱歌霍比舞蹈学校XYZ 242
预期产出:
类型文本Id名称约翰史密斯2548霍比舞蹈学校XYZ 242
发布于 2021-05-04 19:40:11
这是丑陋的,但它是有效的:
class Program
{
static void Main(string[] args)
{
List<Record> records = BuildTestData();
List<Record> deduped = DeDupe(records);
Console.Clear();
foreach (Record r in deduped)
Console.WriteLine($"Type:{r.Typ}, Text:{r.Txt}, ID:{r.ID} ");
Console.ReadKey();
}
static List<Record> DeDupe(List<Record> dupes)
{
List<Record> excludes = new List<Record>();
excludes.AddRange(dupes.GroupBy(x => new { x.Typ, x.Txt }).Where(y => y.Count() > 1).SelectMany(z => z.Where(a => string.IsNullOrEmpty(a.ID))));
excludes.AddRange(dupes.Where(x => !excludes.Any(y => x == y) && dupes.Any(z => x != z && x.Txt != z.Txt && z.Txt.Contains(x.Txt))));
return dupes.Where(x => !excludes.Any(y => x == y)).ToList();
}
static List<Record> BuildTestData()
{
return new List<Record>
{
new Record { Typ = "Name", Txt = "John", ID = null},
new Record { Typ = "Name", Txt = "John", ID = "22"},
new Record { Typ = "Name", Txt = "John Smith", ID = "2548"},
new Record { Typ = "Hobby", Txt = "Singing", ID = null},
new Record { Typ = "Hobby", Txt = "Dancing Singing", ID = null},
new Record { Typ = "School", Txt = "XYZ", ID = null},
new Record { Typ = "School", Txt = "XYZ", ID = "242"},
};
}
}
public class Record
{
public string Typ { get; set; }
public string Txt { get; set; }
public string ID { get; set; }
}https://stackoverflow.com/questions/67390281
复制相似问题