Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to avoid redundant List using LINQ
    primarykey
    data
    text
    <p>I have a Report object that has Recipients property (of String datatype). The Recipients property will hold all the recipients’ email address in <code>comma separated string</code>. I need to create a “<strong>Collection</strong>” of Email objects from the comma separated string. I have the following code that uses a <strong>List of string</strong> to get the email address first. Then I create a collection of email objects. </p> <p>Is there a better way to avoid the redundant List and Collection using <code>LINQ</code>?</p> <pre><code> Report report = new Report(); report.Recipients = "test@test.com, demo@demo.com"; List&lt;string&gt; emailAddressList = new List&lt;string&gt;( report.Recipients.Split(',') ); Collection&lt;Email&gt; emailObjectCollection = new Collection&lt;Email&gt;(); foreach (string emailAddress in emailAddressList) { Email email = new Email(); email.EmailAddress = emailAddress; emailObjectCollection.Add(email); } </code></pre> <p><strong>References</strong>:</p> <ol> <li><a href="https://stackoverflow.com/questions/13247001/better-code-for-avoiding-one-dictionary-case-sensitivity-issue">Better code for avoiding one dictionary - Case Sensitivity Issue</a></li> <li><a href="https://stackoverflow.com/questions/1606679/remove-duplicates-in-the-list-using-linq">Remove duplicates in the list using linq</a></li> <li><a href="https://stackoverflow.com/questions/5596604/using-linq-to-find-duplicates-across-multiple-properties">Using LINQ to find duplicates across multiple properties</a></li> <li><a href="https://stackoverflow.com/questions/1232108/c-difference-between-listt-and-collectiont-ca1002-do-not-expose-generic">C#: Difference between List&lt;T&gt; and Collection&lt;T&gt; (CA1002, Do not expose generic lists)</a></li> </ol> <blockquote> <p><strong>CA1002</strong>: 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. <a href="http://msdn.microsoft.com/en-us/library/ms182142(v=vs.80).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms182142(v=vs.80).aspx</a></p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload