首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >查找带有检查的超级集的Linq查询

查找带有检查的超级集的Linq查询
EN

Stack Overflow用户
提问于 2021-05-04 18:33:12
回答 1查看 80关注 0票数 0

我想用一些重复的文本和/没有id值来查询一个列表。

我有两个条件要匹配,

  1. 任意2个相同的文本,选择带有ID的组,否则选择唯一的文本。
  2. 任意2个文本,其中一个包含另一个文本,选择超级设置文本。唱歌,跳舞唱歌=>跳舞唱歌

类型文本Id名称约翰名字约翰22名约翰史密斯2548霍比唱歌霍比舞蹈学校XYZ 242

预期产出:

类型文本Id名称约翰史密斯2548霍比舞蹈学校XYZ 242

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-04 19:40:11

这是丑陋的,但它是有效的:

代码语言:javascript
运行
复制
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; }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67390281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档