使用Linq每次迭代选择多个项目?

时间:2016-05-25 15:42:53

标签: c# linq

每次迭代,此查询为每个填充的地址创建一个EmailRecipient。可以在没有多次迭代的情况下完成吗?

var addedRecipients = (from oldRecip in oldEmailRecipients
                        where !string.IsNullOrWhiteSpace(oldRecip.EmailAddress1)
                        select new EmailRecipient
                            {
                                UserName = oldRecip.UserName,
                                EmailAddress = oldRecip.EmailAddress1
                            }
                ).Union(from oldRecip in oldEmailRecipients
                        where !string.IsNullOrWhiteSpace(oldRecip.EmailAddress2)
                        select new EmailRecipient
                            {
                                UserName = oldRecip.UserName,
                                EmailAddress = oldRecip.EmailAddress2
                            });

4 个答案:

答案 0 :(得分:3)

您可以使用SelectMany扩展方法:

var addedRecipients = oldEmailRecipients.SelectMany(e=>
                                         {
                                          var result= new List<EmailRecipient>();
                                          if(!string.IsNullOrWhiteSpace(e.EmailAddress1))
                                          {
                                             result.Add(new EmailRecipient
                                                            {
                                                              UserName = e.UserName,
                                                              EmailAddress = e.EmailAddress1
                                                            });
                                          }
                                          if(!string.IsNullOrWhiteSpace(e.EmailAddress2))
                                          {
                                             result.Add(new EmailRecipient
                                                            {
                                                                UserName = e.UserName,
                                                                EmailAddress = e.EmailAddress2
                                                            });
                                          }
                                          return result;
                                         });

更新

我上面展示的解决方案仅适用于Linq to Objects。您的评论建议我使用EF。一个简单的解决方案是在AsEnumerable之前调用SelectMany方法以切换到Linq to Objects,但如果您没有先过滤收件人,则可能会损害您的性能。

另一种解决方案可能是在调用SelectMany之前只选择您首先需要从服务器获取的数据,以便不加载在这种情况下您不需要的其他列:

...Where(...)
  .Select(r=>new{UserName=r.UserName,
                 EmailAddress1=r.EmailAddress1,
                 EmailAddress2=r.EmailAddress2 })
  .AsEnumerable()
  .SelectMany(...);

答案 1 :(得分:1)

坚持使用查询语法,并确保只处理具有非null /空格oldEmailRecipients或非null /空格EmailAddress1的{​​{1}}项:

EmailAddress2

答案 2 :(得分:1)

您可以使用SelectMany构建内联数组以添加两封电子邮件并将其展平。

var addedRecipients = from oldRecip in oldEmailRecipients
    let emails =
        new[] {oldRecip.EmailAddress1, oldRecip.EmailAddress2}.Where(e => !string.IsNullOrWhiteSpace(e))
    from email in emails
    where emails.Any()
    select new EmailRecipient
    {
        UserName = oldRecip.UserName,
        EmailAddress = email
    };

答案 3 :(得分:0)

当您的EmailRecipient有两个以上的电子邮件地址时,您可以执行此操作:

// Just building a pseudo dataclass
List<Recipient> oldEmailRecipients = Enumerable.Range(1, 10).Select(item => new Recipient()
{
    Name = "Recipient" + item,
    EmailAddress1 = "pseudo" + item + "@gmail.com",
    EmailAddress2 = "pseudo" + (item + 1) + "@gmail.com",
    //EmailAddress3 = "pseudo" + (item + 2) + "@gmail.com",
    EmailAddress3 = "",
    EmailAddress4 = "pseudo" + (item + 3) + "@gmail.com",
} ).ToList( )

// create anonymous object for each recipient and a list of valid adresses
var query = from mailRecipients in oldEmailRecipients
            select new
            {
                Name = mailRecipients.Name,
                Addresses = new List<string>()
                {
                    mailRecipients.EmailAddress1,
                    mailRecipients.EmailAddress2,
                    mailRecipients.EmailAddress3,
                    mailRecipients.EmailAddress4
                }.Where(item => string.IsNullOrEmpty( item ) == false )
            };

// create an EmailRecipient for each valid combination
var final = from item in query
            from address in item.Addresses
            select new EmailRecipient
            {
                Name = item.Name,
                Address = address
            };