简体   繁体   中英

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?

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

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:

// 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 :

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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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