我有一个Report对象,它具有Recipients属性(String数据类型)."收件人"属性将保留所有收件人的电子邮件地址comma separated string.我需要从逗号分隔的字符串创建电子邮件对象的" 集合 ".我有以下代码使用字符串列表 来获取电子邮件地址.然后我创建了一个电子邮件对象的集合.
有没有更好的方法来避免冗余的List和Collection使用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);
}
Run Code Online (Sandbox Code Playgroud)
参考文献:
CA1002:不要公开通用列表.System.Collections.Generic.List是一个通用集合,专为性能而非继承而设计,因此不包含任何虚拟成员.http://msdn.microsoft.com/en-us/library/ms182142(v=vs.80).aspx
你可以只使用一个HashSet.
var emailObjectCollection = new HashSet<Email>(
report.Recipients.Split(',').Select(e => new Email() { EmailAddress = e }));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
520 次 |
| 最近记录: |