简体   繁体   English

列出包含字符串值的项目

[英]List items that contain a string value

I have a list of objects that has a string for phone number, I want to create a query to look for a list of objects that has any of the numbers. 我有一个包含电话号码字符串的对象列表,我想创建一个查询以查找具有任何数字的对象列表。

here is the model: 这是模型:

public class ReportViewModel
    {
        public int QueueReportId { get; set; }
        public string Message { get; set; }
        public string PhoneNumber { get; set; }
        public bool Sent { get; set; }
        public DateTime Day { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime SentDate { get; set; }
    }

and I send a list of ReportViewModel to the view, is there a way to sort that list that is being sent to the view by phone number if the phone number is not an exact match? 我将ReportViewModel的列表发送到视图,如果电话号码不完全匹配,是否可以通过电话号码对发送到视图的列表进行排序?

for example, I would like to search on the list for objects that have a phone number that contains the area code 513 anywhere on the public string PhoneNumber { get; set; } 例如,我想在列表中搜索对象,这些对象的电话号码在public string PhoneNumber { get; set; }任何地方都包含区号513 public string PhoneNumber { get; set; } public string PhoneNumber { get; set; }

I know how to look for items with that are exactly the same, but not for something like this. 我知道如何寻找与之完全相同的物品,但不寻找类似物品。

Any help would be appreciated. 任何帮助,将不胜感激。

You could use the Any() method and pass an expression to check it in the PhoneNumber string property with the Contains string method, for sample: 您可以使用Any()方法并传递一个表达式,以使用Contains字符串方法在PhoneNumber字符串属性中对其进行检查,例如:

// get your list
List<ReportViewModel> reports = GetReports();

string areaCode = "513";

// check if any object has a phone value in any part of string
if (reports.Any(r => r.PhoneNumber.Contains(areaCode)))
{
   // contains the phone number
}

Sounds like you want to use LINQ's Where : 听起来好像您想使用LINQ的Where

List<ReportViewModel> reports = //....

foreach(var report in reports.Where(r => r.PhoneNumber.Contains("513"))
{
    //do something for each report
}

Do you mean by something like : 您是说类似的意思吗?

 List<string> areaCode= new List<string>() { "somecode123", "somecode321", "somecode456", };

And you want to find a match of any item from areaCode list on the modular "ReportViewModel". 您想从模块化“ ReportViewModel”上的areaCode列表中找到任何项目的匹配项。

If yes then this should work : 如果是,那么这应该工作:

var records = new List<ReportViewModel>;

var results = records.Where(q => areaCode.Any(t => q.PhoneNumber.Conatins(t)));

This should help, please match the bracket. 这会有所帮助,请匹配括号。

If I undersatand your questions correctly is there a way to sort that list that is being sent to the view by phone number if the phone number is not an exact match? 如果我对您的问题的is there a way to sort that list that is being sent to the view by phone number if the phone number is not an exact match?正确is there a way to sort that list that is being sent to the view by phone number if the phone number is not an exact match? Here is how you can do it to sort the list if the phone number doesn't match in the list. 如果电话号码与列表中的电话号码不匹配,可以按照以下方法对列表进行排序。

public ActionResult Index()
{
    ....
    var reportsvm = GetReports();
    if (!reportsvm.Any(x => x.PhoneNumber.Contains("513")))
    {
         reportsvm = reportsvm.OrderBy(x => x.PhoneNumber).ToList();
         // reportsvm = reportsvm.OrderByDescending(x => x.PhoneNumber).ToList(); // if you want in descending order
    }

   return View(reportsvm);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM