I'm writing am application that I've previously posted questions on here, but I'l looking for some more similar advice of possible. I am looking for how best to write my code, as I have a working example, but to me feels long winded. I'm new to LINQ.
The Application first grabs 3 tables via DataContext:-
var contacts = db.GetTable<Contact>();
var distributionLists = db.GetTable<DistributionList>();
var JunctionTable = db.GetTable<ContactsDistribution>();
I then get the selected items from my listbox, these are distribution lists. A contact can be in more than one distribution list, so i first get the raw data from my database:-
var listBoxItems = listBox1.SelectedItems.Cast<object>().Select(t => t.ToString());
var initialList = (from j in JunctionTable
where listBoxItems.Contains(j.DistributionName)
join c in contacts on j.ContactID equals c.ContactID
select new { c.ContactID,c.Surname,j.DistributionName, j.EmailFlag, j.SMSFlag}).ToList();
I then need to search the initialList collection for users who require Email Notifications, I also need to remove duplicate entries, as a Contact can appear multiple times. So the list is made Distinct:-
var email = (from l in initialList
where l.EmailFlag.Equals(true)
select new { l.ContactID }).Distinct().ToList();
I then do the same search for Contacts that require SMS Notification from the selected lists :-
var sms = (from l in initialList
where l.SMSFlag.Equals(true)
select new { l.ContactID }).Distinct().ToList();
Now that i have lists for both SMS & Email Notifications I need get the required Email Address or mobile number by doing this :-
var smsMobileNumbers = (from s in sms
join c in contacts on s.ContactID equals c.ContactID
select new { c.MobileNumber }).ToList();
var emailAddresses = (from m in email
join c in contacts on m.ContactID equals c.ContactID
elect new { c.EmailAddress }).ToList();
My Question would be, Is there a cleaner way of writing this code or a better, more efficient way of achieving this.