gpt4 book ai didi

c# - 通过 LINQ 排序列表数据

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:25 25 4
gpt4 key购买 nike

我正在尝试通过这种方式订购数据

        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();

数据显示在网格中,但我期望数据不会出现。

输出即将到来

ID  Title

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

JCB 应该排在所有 GEO 之后,因为我正在使用搜索词对数据进行排序,例如“geo jcb” .在大多数行的标题中找到 geo。所以那些出现我的搜索词的行排在第一位。所以 geo 是我的搜索词之一,它在所有行标题中显示最长时间。下一个 jcb 应该出现,因为 jcb 在我的搜索词中,但在最后出现的输出 jcb 相关行中。所以告诉我如何更改我的 linq 查询。谢谢

最佳答案

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

关于c# - 通过 LINQ 排序列表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11415371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com