Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq根据逗号分隔列表中重复的标记数查询自定义订单_C#_Arrays_Linq_Lambda - Fatal编程技术网

C# Linq根据逗号分隔列表中重复的标记数查询自定义订单

C# Linq根据逗号分隔列表中重复的标记数查询自定义订单,c#,arrays,linq,lambda,C#,Arrays,Linq,Lambda,我有一个如下的清单 ID Tags Name 1 acronym, address one 2 applet, abbr, embed two 3 iframe, abbr three 4 abbr, bgsound, bdo four 5 img, acronym five 我想根据最重复

我有一个如下的清单

  ID        Tags                 Name
  1   acronym, address            one
  2   applet, abbr, embed         two
  3   iframe, abbr                three
  4   abbr, bgsound, bdo          four
  5   img, acronym                five
我想根据最重复的标签(如下所示)按顺序获取列表

  ID        Tags                 Name      
  2   applet, abbr, embed         two
  3   iframe, abbr                three      
  4   abbr, bgsound, bdo          four
  1   acronym, address            one
  5   img,  acronym               five
请帮助我如何使用LINQ中的c#lambda表达式获取它。

您只需要两个步骤:

  • 根据重复次数对每个
    标签进行分组。i、 e:
    {“Key”:“abbr”,“Value”:3}
  • 循环遍历数据,然后相应地获得每个
    标记的最大
    权重
    。之后,您可以通过
    OrderByDescending(p=>p.Weight)
  • 已更新

    以逗号分隔的列表重复标记

    输出

    [
      {"ID":2,"Tags":["applet","abbr","embed"],"Name":"two"},
      {"ID":3,"Tags":["iframe","abbr"],"Name":"three"},
      {"ID":4,"Tags":["abbr","bgsound","bdo"],"Name":"four"},
      {"ID":1,"Tags":["acronym","address"],"Name":"one"},
      {"ID":5,"Tags":["img","acronym"],"Name":"five"}
    ]
    

    最重复的是什么意思?这里标记“abbr”重复了3次,首字母缩略词重复了2次基于最重复的标记我需要订单,您迄今为止尝试了什么?obj.OrderBy(item=>preferences.Contains(item.DocumentTags)).ToList();这里的首选项是我需要的顺序列表,例如:list=newlist(){'abbr','acronym'};但结果并没有达到预期效果。
    var mostFrequentTag=myList.SelectMany(item=>item.Tags.GroupBy(tag=>tag.OrderByDescending(chunk=>chunk.Count()).FirstOrDefault().Key这是否回答了您的问题?如果你需要任何帮助,请告诉我@Chintu
    
    var orderedKeyMapping = data.SelectMany(p => p.Tags.Split(',')).Select(x => x.Trim())
                                    .GroupBy(tag => tag)
                                    .ToDictionary(g => g.Key.ToString(), g => g.Count())
                                    .OrderByDescending(p => p.Value);
            var result = data.Select(p => new 
                                     { 
                                         SampleData = p,  
                                         Weight = orderedKeyMapping.FirstOrDefault(o => p.Tags.Split(',').Select(x => x.Trim()).Any(tag => o.Key.Equals(tag))).Value
                                     }).OrderByDescending(p => p.Weight)
                                       .Select(p => p.SampleData); 
    
            Console.WriteLine(JsonConvert.SerializeObject(result)); 
    
    [
      {"ID":2,"Tags":["applet","abbr","embed"],"Name":"two"},
      {"ID":3,"Tags":["iframe","abbr"],"Name":"three"},
      {"ID":4,"Tags":["abbr","bgsound","bdo"],"Name":"four"},
      {"ID":1,"Tags":["acronym","address"],"Name":"one"},
      {"ID":5,"Tags":["img","acronym"],"Name":"five"}
    ]