0

Is there a way to add multiple rows with the same content without loop?

The following is the code I am currently using to achieve this.

        DataTable dtMessageDetails = new DataTable("Private Message Details");
        for (int i = 0; i < 10; i++)
        {
            dtMessageDetails.Rows.Add("65E6BD38-2806-S15G-9DC5-9DE908333996", 3, 0, "News", "News", Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")), "Publish", "mes", 0, 0, 0, "null", "null", "Active");
        }

And my datatable should looks like

enter image description here

I want to replace loop with some other approach may be LINQ.

7
  • 2
    What's wrong with loop? Commented Oct 30, 2013 at 10:16
  • I think I have to be more clear. I need fastest way to achieve that because the datatable will have more than 10000 rows. So for better performance, I like to avoid looping. Commented Oct 30, 2013 at 10:21
  • I think performance will not be an issue. Commented Oct 30, 2013 at 10:24
  • Linq will not be faster than simple loop (I think nothing will be faster than for loop). One question - why you need to create datatable with 10000 duplicated rows? Commented Oct 30, 2013 at 10:25
  • 2
    @Sudha: I guess what you're looking for is the most efficient way to insert duplicate rows in the database, since it is not possible to perform that without loops. A loop has got to exist somewhere! even if it is not in your code it will exist somewhere else (in a library or even in hardware!). Commented Oct 30, 2013 at 10:32

2 Answers 2

3

Linq is for querying data, not for inserting. You can prepare data with linq, but you will use same loop for adding rows, which will not be any faster. Thus you are inserting same data, I suggest you to prepare these data instead of creating new items array on each iteration:

DataTable dtMessageDetails = new DataTable("Private Message Details");
object[] items = { "65E6BD38-2806-S15G-9DC5-9DE908333996", 3, 0, "News", "News", 
                   Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")), 
                   "Publish", "mes", 0, 0, 0, "null", "null", "Active" };

for (int i = 0; i < 10; i++)    
    dtMessageDetails.Rows.Add(items);
Sign up to request clarification or add additional context in comments.

Comments

2

In addition to @lazyberezovsky's answer, I recommend you to use the DataRowCollection.Add Method (DataRow) method overload, which is slightly faster:

var itemArray = new object[] { 
    "65E6BD38-2806-S15G-9DC5-9DE908333996", 3, 0, "News", "News", 
    Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")),
    "Publish", "mes", 0, 0, 0, "null", "null", "Active" };

for (int i = 0; i < 10000; i++)
{
    var r = dtMessageDetails.NewRow();
    r.ItemArray = itemArray;
    dtMessageDetails.Rows.Add(r);
}

If you intend to add the same rows to a new DataTable instance through your code, you might also get some performance improvement if you use the DataTable.Copy Method. Just create the rows above once in your code, then use the following to populate another datatable:

var dtMessageDetailsCopy = dtMessageDetails.Copy();

1 Comment

Didn't know about NewRecordFromArray beast. Good point, skipping items array check will be faster

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.