简体   繁体   English

DataTable中的C#组DataRows

[英]C# Group DataRows in DataTable

I have a DataTable with two columns: File & Email 我有一个包含两列的DataTable表: FileEmail

C://file1.jpg aaa@gmail.com  
C://file2.jpg aaa@gmail.com  
C://file3.jpg bbb@gmail.com  
C://file4.jpg ccc@gmail.com  
C://file5.jpg bbb@gmail.com

In my code i loop through the DataRow s and send an email to Email with File attached. 在我的代码中,我遍历DataRow并将电子邮件发送到带有File Email

Problem: 问题:

I need to somehow check if there are any other DataRow s with the same Email and if so, send just one email with multiple attachments. 我需要以某种方式检查是否存在具有相同Email其他DataRow ,如果是,则仅发送一封包含多个附件的电子邮件。

So the above DataTable would result in 3 emails: 因此,上面的DataTable将产生3封电子邮件:

file1,file2 sent to aaa@gmail.com  
file3,file5 sent to bbb@gmail.com  
file4 sent to ccc@gmail.com

My code sample: 我的代码示例:

foreach (DataRow row in dt.Rows) {
     string file = row[0].ToString();
     string email = row[1].ToString();
     SendEmailWithAttachments(email,file);
}

I could pass a StringCollection or an array to my SendEmailWithAttachments() function then loop throught it and attach all the files, but how do I group these DataRow s in first place 我可以将StringCollection或数组传递到我的SendEmailWithAttachments()函数,然后遍历它并附加所有文件,但是如何首先将这些DataRow分组

Using the GroupBy Linq extension you could handle your DataRows grouping them for the Email field, then foreach create a list of strings with the file names. 使用GroupBy Linq扩展名,您可以处理DataRows以将它们分组为“电子邮件”字段,然后foreach创建带有文件名的字符串列表。

Of course, you need also to change the SendMailWithAttachments to receive as second parameter a List<string> instead of a single string 当然,您还需要更改SendMailWithAttachments以将List<string>而不是单个字符串作为第二个参数来接收

var g = dt.AsEnumerable().GroupBy(d => d.Field<string>("Email"));
foreach (var x in g)
{
    List<string> files = new List<string>();
    foreach (var z in x)
        files.Add(z.Field<string>("File"));

    SendEmailWithAttachments(email,files);

}

You could use GroupBy to group by email: 您可以使用GroupBy通过电子邮件进行分组:

        DataTable dt = new DataTable();
        dt.Columns.Add("Path");
        dt.Columns.Add("Email");
        DataRow dr = dt.NewRow();
        dr.ItemArray=new object[2]{"C://file1.jpg", "aaa@gmail.com"};
        dt.Rows.Add(dr);
        dr = dt.NewRow();

        dr.ItemArray=new object[2]{"C://file2.jpg", "aaa@gmail.com"};
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr.ItemArray=new object[2]{"C://file3.jpg", "bbb@gmail.com"};
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr.ItemArray=new object[2]{"C://file4.jpg", "ccc@gmail.com"};
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr.ItemArray=new object[2]{"C://file5.jpg", "bbb@gmail.com"};
        dt.Rows.Add(dr);


        var grouped=dt.AsEnumerable().GroupBy(x=>x.Field<string>("Email"));

        foreach (var mail in grouped)
        {
            List<string> filesForEmail = new List<string>();
            foreach (var file in mail)
            {
                filesForEmail.Add(file.Field<string>("Path"));
            }

            SendEmailWithAttachments(mail.Key, filesForEmail);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM