搜索列表和订单按值最大找到列表c# [英] Search list and order List by value max found c#

查看:71
本文介绍了搜索列表和订单按值最大找到列表c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢承认我在LINQ方面很虚弱. 我有数据清单.我想按给定值搜索列表拳头,然后按最大出现次数对数据进行排序,这意味着出现在行中的时间最长.

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

所有cat都将排名第一,因为此cat关键字在所有行中都找到了最大时间,然后在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

寻找解决方案.谢谢

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

我尝试在其他应用中使用您的逻辑,但无法正常工作.根据逻辑,三行首先带有GEO关键字,然后接下来的2行带有JCB,然后是无与伦比的其余部分.我需要更改您的代码.请帮忙.谢谢

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

推荐答案

这将过滤您的列表并按Title将其分组,并按分组大小对分组进行排序.

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

输出:

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.

(请注意,此代码将忽略Title的情况.我不知道这是您想要的还是代码中的错字:您正在使用catCat,在您的输出只有Cat,但dog没有大写.)

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

要获取不匹配的项目,可以使用例外:

To get the unmatched items, you can use Except:

var unmatched = list.Except(result.SelectMany(m => m)); // beware! contains the tiger!

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

输出

ID=1,Title=Cat
ID=4,Title=Cat
ID=5,Title=cat
ID=2,Title=dog
ID=6,Title=dog
ID=3,Title=Tiger

编辑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;

这篇关于搜索列表和订单按值最大找到列表c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆