5

I have a Report object that has Recipients property (of String datatype). The Recipients property will hold all the recipients’ email address in comma separated string. I need to create a “Collection” of Email objects from the comma separated string. I have the following code that uses a List of string to get the email address first. Then I create a collection of email objects.

Is there a better way to avoid the redundant List and Collection using LINQ?

  Report report = new Report();
  report.Recipients   = "test@test.com, demo@demo.com";

  List<string> emailAddressList = new List<string>( report.Recipients.Split(',') );
  Collection<Email> emailObjectCollection = new Collection<Email>();

  foreach (string emailAddress in emailAddressList)
  {
           Email email = new Email();
           email.EmailAddress = emailAddress;
           emailObjectCollection.Add(email);
  }

References:

  1. Better code for avoiding one dictionary - Case Sensitivity Issue
  2. Remove duplicates in the list using linq
  3. Using LINQ to find duplicates across multiple properties
  4. C#: Difference between List<T> and Collection<T> (CA1002, Do not expose generic lists)

CA1002: Do not expose generic lists. System.Collections.Generic.List is a generic collection designed for performance not inheritance and, therefore, does not contain any virtual members. http://msdn.microsoft.com/en-us/library/ms182142(v=vs.80).aspx

Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • 1
    Why do you need to use `Collection` at all? It's designed to be a *base* class... what advantage does it have for you over `List`? – Jon Skeet Nov 29 '12 at 14:56
  • @JonSkeet I need to return the collection in a publicly exposed method. If I use `List`, I will get `CA1002` warning in code analysis. Is it a bad idea to return a `Collection`? If yes, how should I modify the code to use collection in proper way? – LCJ Nov 29 '12 at 15:11
  • @Lijo Just don't use `Collection` at all. Create a `List` and return a `List`. – Servy Nov 29 '12 at 15:12
  • 1
    @Lijo Well, first off, it's just a warning, there is nothing to overcome. Second, reading it's text, I disagree with what it's asserting; a `List` is a perfectly acceptable type to return, and obscuring it's type by exposing only a collection that provides no indication as to it's implementation is harmful to the caller; they won't know how to properly use the collection. – Servy Nov 29 '12 at 15:16

3 Answers3

4

You can just use a HashSet.

var emailObjectCollection = new HashSet<Email>(
     report.Recipients.Split(',').Select(e => new Email() { EmailAddress = e }));
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
4
var  emailObjectCollection =   report.Recipients.Split(',')
                                    .Select(m=>new Email(){EmailAddress = m})
                                    .ToList()
L.B
  • 114,136
  • 19
  • 178
  • 224
1

Just off the top of my head:

var emailObjectCollection = new Collection<Email>(
    new List<string>(report.Recipients.Split(',')).Distinct()
        .Select<string, Email>(e => 
        {
            var email = new Email();
            email.EmailAddress = e;
        }).ToList());

That should be the LINQ-y way of populating a Collection<T> with unique values. Just curious though, why use a Collection? If you need to pass this enumerable around, would it not make more sense to use IEnumerable?

Plus, you could suppress the CA1002 message with a suppression attribute - there are valid exceptions to the rule, from time to time (IMHO).

code4life
  • 15,655
  • 7
  • 50
  • 82