I have a class that models a person; among the properties apart from the usual is that person's mobile number
public class Person
{
// Other properties elided here for brevity's sake
public string PersonMobileNumber { get; set; }
}
That person is sent text messages that they may or may not respond to. I've defined a message thus:
public class SmsMessage
{
public string NumberFrom { get; set;}
public string NumberTo { get; set;}
public string MessageContent { get; set; }
public DateTime DateTimeReceived { get; set; }
}
Now what I want to see is who's responded to my text messages.
In plain English you have a number of SMS messages (List<SmsMessage>
) and some people (List<Person>
)
In the old pre-LINQ days I'd simply have foreached
my way through both collections, with one foreach
inside the first but I thought that LINQ should be able to help me here. The relevant statement that I've come up with looks as follows:
List<Person> peopleThatResponded =
people.Where(p => smsMessages.Exists(s => s.NumberFrom == p.MobileNumber)).ToList();
It just feels that this will work fine for small data sets (this is based on debugging it and watching the cycles through the code) but I'm sure that there's a more performant way to do this. FWIW I have complete control over the class definitions and can make any changes that need making.