[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
SolvedPrivate

Update a DataTable after adding two new columns using linq

Posted on 2014-08-26
1
Medium Priority
?
33 Views
Last Modified: 2016-02-15
Greetings experts.
I have a DataTable which under certain conditions I must add two columns. I also must set these columns and was hoping I could do so in LINQ instead of iterating through the entire row set.
if (!String.IsNullOrEmpty(myParam))
            {
                table.Columns.Add("COL1", typeof(string));
                table.Columns.Add("COL2", typeof(string));

                table.AsEnumerable().ForEach(r =>
                   r["COL1"] = r["COL2"] = myParam);

                table.AcceptChanges();
            }

Open in new window

I am successfully adding the columns but the values remain null.
Any help on syntax would be greatly appreciated.
0
Comment
Question by:Focker513
1 Comment
 

Accepted Solution

by:
Focker513 earned 0 total points
ID: 40286799
Disregard, found the answer here:
http://stackoverflow.com/questions/19855772/update-two-columns-in-a-datatable-using-linq

if (!String.IsNullOrEmpty(myParam))
            {
                table.Columns.Add("COL1", typeof(string));
                table.Columns.Add("COL2", typeof(string));

                table.AsEnumerable().ToList<DataRow>().ForEach(r =>
                   r["COL1"] = r["COL2"] = myParam);

                table.AcceptChanges();
            }

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question