Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

share|improve this question

1 Answer

The only change I would make would be to remove the two statements where you create the lists sms and email. They are redundant: by adding the EmailAddress and MobileNumber to the query where you are creating the initialList, you can do a select on the initialList.

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, c.EmailAddress, c.MobileNumber, 
                                j.DistributionName, j.EmailFlag, j.SMSFlag }).ToList();

I changed these more because I like this syntax better than the SQL like Linq.

var emailAddresses = initialList
    .Where(l => l.EmailFlag)
    .Select(l => l.EmailAddress).Distinct().ToList();

var smsMobileNumbers = initialList
    .Where(l => l.SMSFlag)
    .Select(l => l.MobileNumber).Distinct().ToList();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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