Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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#_Linq - Fatal编程技术网

C# 按LINQ排序列表数据

C# 按LINQ排序列表数据,c#,linq,C#,Linq,通过这种方式,我试图对数据进行排序 List<SearchResult> list = new List<SearchResult>() { new SearchResult(){ID=1,Title="Geo Prism 1995 GEO GEO- ABS #16213899"}, new SearchResult(){ID=2,Title="Geo Prism 1995 GEO - ABS #16213899"},

通过这种方式,我试图对数据进行排序

        List<SearchResult> list = new List<SearchResult>() {
        new SearchResult(){ID=1,Title="Geo Prism 1995 GEO GEO- ABS #16213899"},
        new SearchResult(){ID=2,Title="Geo Prism 1995 GEO - ABS #16213899"},
        new SearchResult(){ID=3,Title="Geo Prism 1995 - ABS #16213899"},
        new SearchResult(){ID=3,Title="Geo Prism 1995 - ABS #16213899"},
        new SearchResult(){ID=4,Title="Wie man BBA reman erreicht"},
        new SearchResult(){ID=5,Title="Ersatz Airbags, Gurtstrammer und Auto Körper Teile "},
        new SearchResult(){ID=6,Title="JCB Excavator - ECU P/N: 728/35700"},
        };

        var to_search = new[] { "Geo", "JCB" };
        var result = from searchResult in list
                     let title = searchResult.Title.ToLower()
                     let key_string = to_search.FirstOrDefault(ts => title.Contains(ts))
                     orderby key_string == null ? -1 : title.Split(new[] { key_string }, StringSplitOptions.None).Length descending
                     group searchResult by key_string into Group
                     orderby Group.Count() descending
                     select Group;

        var matched = result.SelectMany(m => m);
        var completeList = matched.Concat(list.Except(matched));
        dataGridView2.DataSource = completeList.ToList();//.SelectMany(x => x).ToList();

但我想在下面这样展示数据 输出应该是
JCB应该在所有GEO之后出现,因为我使用搜索词对数据进行排序,如
“geo jcb”。在大多数行的标题中都可以找到geo。所以这些行首先出现在我的搜索词中。所以geo是我的搜索词之一,它在所有标题行中显示最长时间。下一个jcb应该出现,因为jcb在我的搜索词中,但在最后出现的输出jcb相关行中。因此,请告诉我如何更改linq查询。谢谢

嗯?他不想按头衔排序。Ersatz位于Geo之后。修复了新发现的阅读理解/理解问题。这一行给出了错误。错误消息是“组在当前上下文中不存在”OrderByDescending(group=>group.Count())。然后是ByDescending(CountStringOccents(group.Key,to_search));抱歉,团体是保留字。将“组”重命名为另一个名称。我对你的目标感到困惑。我最初误解了你的问题,但看起来你想要所有的项目,但你想要“匹配”的项目过滤到顶部?同样,这些行将排在第一位,这将有最大的单词出现率。第一个排序将类似于在行中找到的搜索词的最长时间,然后这些行将排在第一位。下一步再次排序,比如哪个排序项在标题中找到的最长时间将排在第一位。需要有两个排序。是的,我想先对匹配项进行extarct排序,然后对匹配项进行排序…然后在末尾用不匹配项对数据进行排序。我想你问这个问题已经问了很多次了。我非常抱歉hogan,我很抱歉在这个问答中提出了一些类似的问题。我从来没有与linq合作过一些使用非常基本的问题但我的要求非常复杂……这就是我失败、失败和沮丧的原因。把这些Q/S放在这里。再次抱歉,谢谢你的耐心。你的输出看起来不错。现在它正在工作。如果出现任何问题,在我的工作中实施后,我会返回给你。thaks a loti在执行你的查询后非常高兴,但当我对巨大的数据执行查询时,我变得非常失望,然后它没有完美地工作。当我搜索像“奥迪地理”这样的关键词时,奥迪获得了巨大的数据。结果是从奥迪开始的…没关系,但奥迪记录结果应该以关键字geo开始,但在奥迪其他记录显示后,然后记录也带有geo和其他记录。所以我不明白为什么当列表中有大量数据时,这种行为会很奇怪。如果你明白我想说的话,你能解释一下吗。谢谢
    var result = (from searchResult in list
                 let title = searchResult.Title.ToLower()
                 let key_string = to_search.FirstOrDefault(ts => title.Contains(ts))
                 orderby key_string == null ? -1 : title.Split(new[] { key_string }, StringSplitOptions.None).Length descending
                 group searchResult by key_string into Group
                 select Group).OrderByDescending(grp => grp.Count()).ThenByDescending( grp => CountStringOccurrances(grp.Key, to_search));

public static int CountStringOccurrences(string text, string[] pattern)
{
    // Loop through all instances of the string 'text'.
    int count = 0;
    foreach (string itm in pattern)
    { 
       int i = 0;
       while ((i = text.IndexOf(itm, i)) != -1)
       {
          i += itm.Length;
          count++;
        }
    }
    return count;
}
1   Geo Prism 1995 - ABS #16213899
2   Geo Prism 1995 - ABS #16213899
3   Geo Prism 1995 - ABS #16213899
3   Geo Prism 1995 - ABS #16213899
4   Wie man BBA reman erreicht
5   Ersatz Airbags, Gurtstrammer und Auto Körper Teile 
6   JCB Excavator - ECU P/N: 728/35700
ID  Title
1   Geo Prism 1995 GEO GEO - ABS #16213899
2   Geo Prism 1995 GEO - ABS #16213899
3   Geo Prism 1995 - ABS #16213899
3   Geo Prism 1995 - ABS #16213899
6   JCB Excavator - ECU P/N: 728/35700
4   Wie man BBA reman erreicht
5   Ersatz Airbags, Gurtstrammer und Auto Körper Teile 
    var result = (from searchResult in list
                 let title = searchResult.Title.ToLower()
                 let key_string = to_search.FirstOrDefault(ts => title.Contains(ts))
                 orderby key_string == null ? -1 : title.Split(new[] { key_string }, StringSplitOptions.None).Length descending
                 group searchResult by key_string into Group
                 select Group).OrderByDescending(grp => grp.Count()).ThenByDescending( grp => CountStringOccurrances(grp.Key, to_search));

public static int CountStringOccurrences(string text, string[] pattern)
{
    // Loop through all instances of the string 'text'.
    int count = 0;
    foreach (string itm in pattern)
    { 
       int i = 0;
       while ((i = text.IndexOf(itm, i)) != -1)
       {
          i += itm.Length;
          count++;
        }
    }
    return count;
}
class SearchResult{
    public int ID { get; set; } 
    public string Title{ get; set; }
}

void Main()
{

    List<SearchResult> list = new List<SearchResult>() {
        new SearchResult(){ID=4,Title="Wie man BBA reman erreicht"},
        new SearchResult(){ID=5,Title="Ersatz Airbags, Gurtstrammer und Auto Körper Teile "},
        new SearchResult(){ID=6,Title="JCB Excavator - ECU P/N: 728/35700"},
        new SearchResult(){ID=2,Title="Geo Prism 1995 GEO - ABS #16213899"},
        new SearchResult(){ID=3,Title="Geo Prism 1995 - ABS #16213899"},
        new SearchResult(){ID=1,Title="Geo Prism 1995 GEO GEO- ABS #16213899"},
    };

    var to_search = new[] { "Geo", "JCB" };

    var result = from sr in list
                 let w = to_search.FirstOrDefault(ts => sr.Title.ToLower().Contains(ts.ToLower()))
                 where w != null
                 let a = new {sr=sr, word=w.ToLower()}
                 group a by a.word into g
                 orderby g.Count() descending
                 let sorted = g.OrderByDescending(a=> a.sr.Title.Select((c, i) => a.sr.Title.Substring(i)).Count(sub => sub.ToLower().StartsWith(a.word)))
                 from a in sorted 
                 select a.sr;

    var completeList = result.Concat(list.Except(result));

    foreach (var element in completeList)
        Debug.WriteLine(String.Format("ID={0},Title={1}", element.ID, element.Title));
}
ID=1,Title=Geo Prism 1995 GEO GEO- ABS #16213899
ID=2,Title=Geo Prism 1995 GEO - ABS #16213899
ID=3,Title=Geo Prism 1995 - ABS #16213899
ID=6,Title=JCB Excavator - ECU P/N: 728/35700
ID=4,Title=Wie man BBA reman erreicht
ID=5,Title=Ersatz Airbags, Gurtstrammer und Auto Körper Teile