Populate DataTable with LINQ in C#

Posted by RaYell on Stack Overflow See other posts from Stack Overflow or by RaYell
Published on 2009-09-10T12:32:23Z Indexed on 2010/03/18 8:11 UTC
Read the original article Hit count: 326

Filed under:
|
|

I have a method in my app that populates DataTable with the data using the following code:

DataTable dt = this.attachmentsDataSet.Tables["Attachments"];

foreach (Outlook.Attachment attachment in this.mailItem.Attachments)
{
    DataRow dr = dt.NewRow();
    dr["Index"] = attachment.Index;
    dr["DisplayName"] = String.Format(
        CultureInfo.InvariantCulture,
        "{0} ({1})", 
        attachment.FileName,
        FormatSize(attachment.Size));
    dr["Name"] = attachment.FileName;
    dr["Size"] = attachment.Size;

    dt.Rows.Add(dr);
}

I was wondering if I could achieve the same functionality using LINQ in order to shorten this code a bit. Any ideas?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ