本文是小编为大家收集整理的关于搜索列表并按最大发现值排列列表 c#的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我喜欢承认我在Linq中很虚弱. 我有数据列表.我想通过给定值搜索拳头,然后按最大发生的数据对行进行排序,这是在行中最长的时间.
List<SearchResult> list = new List<SearchResult>() { new SearchResult(){ID=1,Title="Cat"}, new SearchResult(){ID=2,Title="dog"}, new SearchResult(){ID=3,Title="Tiger"}, new SearchResult(){ID=4,Title="Cat"}, new SearchResult(){ID=5,Title="cat"}, new SearchResult(){ID=6,Title="dog"}, };
如果我使用" dog cat"之类的数据进行搜索和排序列表,那么输出将就像
ID=1,Title=Cat ID=4,Title=Cat ID=5,Title=Cat ID=2,Title=dog ID=6,Title=dog
所有猫都将首先出现,因为此猫关键字在所有行中找到了最大的时间,然后狗找到了最大的时间.
以下数据不会到来,因为它不在搜索词中
ID=3,Title=Tiger
寻找解决方案.谢谢
更新部分代码
List<SearchResult> list = new List<SearchResult>() { new SearchResult(){ID=1,Title="Geo Prism 1995 - ABS #16213899"}, new SearchResult(){ID=2,Title="Excavator JCB - ECU P/N: 728/35700"}, new SearchResult(){ID=3,Title="Geo Prism 1995 - ABS #16213899"}, new SearchResult(){ID=4,Title="JCB Excavator - ECU P/N: 728/35700"}, new SearchResult(){ID=5,Title="Geo Prism 1995 - ABS #16213899"}, new SearchResult(){ID=6,Title="dog"}, }; var to_search = new[] { "Geo", "JCB" }; var result = list.Where(sr => to_search.Any(ts => String.Compare(ts, sr.Title, StringComparison.OrdinalIgnoreCase) == 0)) .GroupBy(sr => sr.Title.ToLower()) .OrderByDescending(g => g.Count()); var matched = result.SelectMany(m => m); var completeList = matched.Concat(list.Except(matched)); dataGridView2.DataSource = completeList.ToList();
我尝试在另一个应用程序中掌握您的逻辑,但它不起作用.根据逻辑,第三行首先带有地理关键字,然后接下来的2行带有JCB,然后无与伦比的休息出现.我需要更改您的代码.请帮忙.谢谢
推荐答案
这将过滤您的列表并按Title进行分组,对组的大小进行排序.
List<SearchResult> list = new List<SearchResult>() { new SearchResult(){ID=1,Title="Cat"}, new SearchResult(){ID=2,Title="dog"}, new SearchResult(){ID=3,Title="Tiger"}, new SearchResult(){ID=4,Title="Cat"}, new SearchResult(){ID=5,Title="cat"}, new SearchResult(){ID=6,Title="dog"}, }; var to_search = new[] { "cat", "dog" }; var result = list.Where(sr => to_search.Any(ts => String.Compare(ts, sr.Title, StringComparison.OrdinalIgnoreCase) == 0)) .GroupBy(sr => sr.Title.ToLower()) .OrderByDescending(g => g.Count()); foreach (var group in result) foreach (var element in group) Debug.WriteLine(String.Format("ID={0},Title={1}", element.ID, element.Title));
输出:
ID=1,Title=Cat ID=4,Title=Cat ID=5,Title=cat ID=2,Title=dog ID=6,Title=dog
如果您不在乎实际的分组,则可以将 selectany .
(请注意,此代码将忽略Title的情况.我不知道这是您想要的,还是代码中的错字:您正在使用cat和Cat,以及在您的输出仅是Cat,但dog未大写.)
编辑:
问题描述
i like to confess that i am weak in LINQ. i have list with data. i want to search list fist by given value and then sort data by max occurance means which comes maximum time in rows.
List<SearchResult> list = new List<SearchResult>() { new SearchResult(){ID=1,Title="Cat"}, new SearchResult(){ID=2,Title="dog"}, new SearchResult(){ID=3,Title="Tiger"}, new SearchResult(){ID=4,Title="Cat"}, new SearchResult(){ID=5,Title="cat"}, new SearchResult(){ID=6,Title="dog"}, };
if i search & sort list with data like "dog cat" then output will be like
ID=1,Title=Cat ID=4,Title=Cat ID=5,Title=Cat ID=2,Title=dog ID=6,Title=dog
all cat will come first because this cat keyword found maximum time in all the rows and then dog found maximum time.
this below data will not come because it is not in search term
ID=3,Title=Tiger
looking for solution. thanks
UPDATE PORTION CODE
List<SearchResult> list = new List<SearchResult>() { new SearchResult(){ID=1,Title="Geo Prism 1995 - ABS #16213899"}, new SearchResult(){ID=2,Title="Excavator JCB - ECU P/N: 728/35700"}, new SearchResult(){ID=3,Title="Geo Prism 1995 - ABS #16213899"}, new SearchResult(){ID=4,Title="JCB Excavator - ECU P/N: 728/35700"}, new SearchResult(){ID=5,Title="Geo Prism 1995 - ABS #16213899"}, new SearchResult(){ID=6,Title="dog"}, }; var to_search = new[] { "Geo", "JCB" }; var result = list.Where(sr => to_search.Any(ts => String.Compare(ts, sr.Title, StringComparison.OrdinalIgnoreCase) == 0)) .GroupBy(sr => sr.Title.ToLower()) .OrderByDescending(g => g.Count()); var matched = result.SelectMany(m => m); var completeList = matched.Concat(list.Except(matched)); dataGridView2.DataSource = completeList.ToList();
i try to your logic in another apps but it is not working. according to logic three rows first come with GEO keyword and then next 2 rows comes with JCB and then unmatched rest comes. what i need to change in ur code. please help. thanks
推荐答案
This will filter your list and group it by Title, sorting the groups by their size.
List<SearchResult> list = new List<SearchResult>() { new SearchResult(){ID=1,Title="Cat"}, new SearchResult(){ID=2,Title="dog"}, new SearchResult(){ID=3,Title="Tiger"}, new SearchResult(){ID=4,Title="Cat"}, new SearchResult(){ID=5,Title="cat"}, new SearchResult(){ID=6,Title="dog"}, }; var to_search = new[] { "cat", "dog" }; var result = list.Where(sr => to_search.Any(ts => String.Compare(ts, sr.Title, StringComparison.OrdinalIgnoreCase) == 0)) .GroupBy(sr => sr.Title.ToLower()) .OrderByDescending(g => g.Count()); foreach (var group in result) foreach (var element in group) Debug.WriteLine(String.Format("ID={0},Title={1}", element.ID, element.Title));
Output:
ID=1,Title=Cat ID=4,Title=Cat ID=5,Title=cat ID=2,Title=dog ID=6,Title=dog
If you don't care about the actual grouping, just can flatten the list of groups with SelectMany.
(Note that this code will ignore the case of Title. I don't know if this is what you want or if it is a typo in code: you are using cat and Cat, and in your output it is only Cat, but dog is not capitalized.)
Edit:
To get the unmatched items, you can use Except:
var unmatched = list.Except(result.SelectMany(m => m)); // beware! contains the tiger!
Edit 2:
var result = list.Where(sr => to_search.Any(ts => String.Compare(ts, sr.Title, StringComparison.OrdinalIgnoreCase) == 0)) .GroupBy(sr => sr.Title.ToLower()) .OrderByDescending(g => g.Count()); var matched = result.SelectMany(m => m); var completeList = matched.Concat(list.Except(matched)); foreach (var element in completeList) Debug.WriteLine(String.Format("ID={0},Title={1}", element.ID, element.Title));
Output
ID=1,Title=Cat ID=4,Title=Cat ID=5,Title=cat ID=2,Title=dog ID=6,Title=dog ID=3,Title=Tiger
Edit 3
var result = from searchResult in list let key_string = to_search.FirstOrDefault(ts => searchResult.Title.ToLower().Contains(ts.ToLower())) group searchResult by key_string into Group orderby Group.Count() descending select Group;
其他推荐答案
I think the following should do the trick.
var searchText = "cat dog"; var searchResult = list .Select(i => new { Item = i, Count = list.Count(x => string.Compare(x.Title, i.Title, true) == 0) // Add counter }) .Where(i => searchText.Contains(i.Item.Title)) .OrderByDescending(i => i.Count) .ThenBy(i => i.Item.ID) .ToList()
Update
If you want unmatched data at the end, you need to add another sorting property in the anonymous object.
var searchText = "cat dog"; var searchResult = list .Select(i => new { Item = i, Matched = searchText.Contains(i.Item.Title.ToUpper()), Count = list.Count(x => string.Compare(x.Title, i.Title, true) == 0) // Add counter }) .OrderByDescending(i => i.Matched) .ThenBy(i => i.Count) .ThenBy(i => i.Item.ID) .ToList()
其他推荐答案
IEnumerable<string> pets = new[] { "Cat", "dog", "Tiger", "Cat", "cat", "dog" }; var test = pets .Where(p=>p.ToUpperInvariant() == "CAT" || p.ToUpperInvariant() == "DOG") .GroupBy(p => p.ToUpperInvariant()) .OrderByDescending(g => g.Count()) .SelectMany(p => p); foreach (string pet in test) Console.WriteLine(pet);