0

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

I know how to look for items with that are exactly the same, but not for something like this.

Any help would be appreciated.

1
  • 1
    are you familiar with the string.Contains() Method..?
    – MethodMan
    Oct 8, 2014 at 15:49

4 Answers 4

1

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
}
1
  • Thank you! I had to do some modifications to make it fit my code but this is what I needed. All of the other answers are correct too! but I needed to chose the one that was closer to my needs. Oct 8, 2014 at 16:21
1

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
}
1
  • This works also, I just needed to build a new list inside the foreach Oct 8, 2014 at 16:21
1

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.

1
  • @RiquelmyMelara : You might wanna mark it as answer if it helped.
    – foo-baar
    Oct 8, 2014 at 16:21
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.