过滤器列表< object>与list< string>并在C#中寻找匹配项 [英] filter list <object> with list<string> and look for a match in c#

查看:77
本文介绍了过滤器列表< object>与list< string>并在C#中寻找匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表&列表

I have a list & List

我想从dbCars对象中获取Car的列表,条件是 名称,Title,Comment属性包含target_terms数组中的任何值[请注意,我不是在试图找到包含对象Property的数组,而是在尝试反向]即时为结果分配排名

I want to fetch the list of Car from the dbCars object with condition the the Name, Title,Comment properties contains any of the value in target_terms array [ Please note I am not trying to find array contains object Property , but i am trying the reverse] & assign a Rank on the fly to the result

我尝试了下面的代码[它将检查数组是否包含任何属性]

I tried the below code [ which will check array contains any of the properties ]

 List<Car> dbCars;//Values from Db 
 List<string> target_terms=new List<string> {"diesel","mechanic"};
var ranked_result = (from carItem in dbContext.Cars                  
                     select new
                     {
                         carItem,
                         Rank =   target_terms.Contains(carItem.Title) ? 1 :
                                  target_terms.Contains(carItem.Name) ? 2 :                               
                                  target_terms.Contains(carItem.Comment) ? 3 :
                                 0
                     }).OrderBy(i => i.Rank);
 //exclude results with no match (rank=0 ) and get a Distinct set  of images
List<Car> _searchResult = (from item in ranked_result
                             where item.Rank != 0
                             select item.carItem).Distinct().ToList();

我该如何编写相同的代码来完成相反的操作,即检查Car属性是否包含target_terms中的任何单词

How can i write the same code to do the reverse ie to check the Car properties contain any of the words in target_terms

例如,如果我要搜索柴油,并且如果我有2辆名称为"Swift Diesel car","Alto Diesel car"的汽车,则应返回2个结果 即我的财产标题,名称或评论中包含柴油"一词

Example if i am searching for diesel and if i have 2 cars with Name "Swift Diesel car" "Alto Diesel car" it should return the 2 results ie My Properties Title or Name or Comment contains the word "diesel"

推荐答案

您可以这样做

var _searchResult = dbCars.Cars.Where(carItem => target_terms.Any(x => carItem.Title.Contains(x) ||
                                                                       carItem.Name.Contains(x) ||
                                                                       carItem.Comment.Contains(x)));

var _searchResult = dbCars.Cars.Where(carItem => target_terms.Any(x => carItem.Title.Contains(x) ||
                                                                       carItem.Name.Contains(x) ||
                                                                       carItem.Comment.Contains(x)))
                               .Select(carItem => new
                               {
                                   carItem,
                                   Rank = target_terms.Any(x => carItem.Title.Contains(x)) ? 1 :
                                          target_terms.Any(x => carItem.Name.Contains(x)) ? 2 :
                                          target_terms.Any(x => carItem.Comment.Contains(x)) ? 3 : 0
                               })
                               .OrderBy(carItem => carItem.Rank);

希望这对您有帮助...

Hope this helps...

这篇关于过滤器列表&lt; object&gt;与list&lt; string&gt;并在C#中寻找匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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