Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Linq在每次迭代中选择多个项目?_C#_Linq - Fatal编程技术网

C# 使用Linq在每次迭代中选择多个项目?

C# 使用Linq在每次迭代中选择多个项目?,c#,linq,C#,Linq,每次迭代,此查询为每个填充的地址创建一个EmailRecipient。它能在没有多次迭代的情况下完成吗 var addedRecipients = (from oldRecip in oldEmailRecipients where !string.IsNullOrWhiteSpace(oldRecip.EmailAddress1) select new EmailRecipient

每次迭代,此查询为每个填充的地址创建一个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
                            });
您可以使用扩展方法:

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;
                                         });

坚持查询语法,确保只处理具有非空/空白
EmailAddress1
或非空/空白
EmailAddress2
oldEmailRecipients
项目:

var addedRecipients =
    from oldEmail in oldEmailRecipients
    let hasEmail1 = !string.IsNullOrWhiteSpace(oldEmail.EmailAddress1)
    let hasEmail2 = !string.IsNullOrWhiteSpace(oldEmail.EmailAddress2)
    where hasEmail1 || hasEmail2
    let emailUserNameCombos = hasEmail1 && hasEmail2
        ? new[]
        {
            new {Email = oldEmail.EmailAddress1, oldEmail.UserName},
            new {Email = oldEmail.EmailAddress2, oldEmail.UserName}
        }
        : hasEmail1
            ? new[] {new {Email = oldEmail.EmailAddress1, oldEmail.UserName}}
            : new[] {new {Email = oldEmail.EmailAddress2, oldEmail.UserName}}
    from emailUsername in emailUserNameCombos
    select new EmailRecipient 
        {
            UserName = emailUsername.UserName,
            EmailAddress = emailUsername.Email
        };

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

// 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
            };
//只是构建一个伪数据类
列出oldEmailRecipients=Enumerable.Range(1,10)。选择(项=>new Recipient()
{
Name=“收件人”+项目,
EmailAddress1=“pseudo”+item+“@gmail.com”,
EmailAddress2=“pseudo”+(item+1)+“@gmail.com”,
//EmailAddress3=“pseudo”+(item+2)+“@gmail.com”,
电邮地址3=“”,
EmailAddress4=“pseudo”+(第+3项)+“@gmail.com”,
})。托利斯特()
//为每个收件人创建匿名对象和有效地址列表
var query=来自oldEmailRecipients中的mailRecipients
选择新的
{
Name=邮件收件人。Name,
地址=新列表()
{
mailRecipients.EmailAddress1,
mailRecipients.EmailAddress2,
mailRecipients.EmailAddress3,
mailRecipients.EmailAddress4
}.Where(item=>string.IsNullOrEmpty(item)==false)
};
//为每个有效组合创建电子邮件收件人
var final=来自查询中的项
来自项中的地址。地址
选择新的电子邮件收件人
{
名称=项。名称,
地址
};

您可以构建一个内联数组来添加这两封电子邮件,并使用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
    };

如果
EmailAddress1
EmailAddress2
都有特定收件人的值,该怎么办?是否要为该收件人获取1或2个
EmailRecipient
结果?如果已填充EmailAddress1和EmailAddress2,则为每个收件人发送一个EmailRecipient。因此,每个旧EmailRecipient可以有0、1或2次发射。10行可能会生成20个EmailRecipients。在这种情况下,octavioccl的答案就是您所需要的好答案+1。您可能希望格式化答案,使其更具可读性。简洁明了。除了“带有语句体的lambda表达式不能转换为表达式树。”可能是我这边的问题。哦,你在使用EF吗?请downvoter,你能解释一下为什么要改进我的答案吗?@octavioccl仅供参考,我只是给你投了更高的票!计算出它弹出后的可计算分钟数是由于EF。你的回答是精心设计的。
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
    };