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:
- Better code for avoiding one dictionary - Case Sensitivity Issue
- Remove duplicates in the list using linq
- Using LINQ to find duplicates across multiple properties
- 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