Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use the ASEnumerable() funtion, but dataset.datatable is invalid argument error occur. i include System.linq; namespace and system.data.extensions.dll and the needed dll to my project but still show this error

i will try this code
C#
List<datatable> result = DTHead.AsEnumerable()
            .GroupBy(row => row.Field<int>("MIVID"))
            .Select(g => g.CopyToDataTable())
            .ToList();
Posted
Updated 19-Aug-16 1:45am
v2
Comments
Karthik_Mahalingam 19-Aug-16 1:52am    
DTHead is datatable or dataset?
Raja Ganapathy 19-Aug-16 1:57am    
Datatable
Karthik_Mahalingam 19-Aug-16 2:22am    
Always use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.
Raja Ganapathy 19-Aug-16 3:05am    
DS_test.Tables[0].Columns.Count;

how to split column count wise.
Karthik_Mahalingam 19-Aug-16 2:27am    
tested the code, its not throwing any exception, might be you are missing something and you are not telling.

Well... Your code looks good. I tested above query and it works as expected. See:
C#
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("Note", typeof(string)));

dt.Rows.Add(new object[]{1, "A"});
dt.Rows.Add(new object[]{1, "B"});
dt.Rows.Add(new object[]{1, "C"});
dt.Rows.Add(new object[]{2, "D"});
dt.Rows.Add(new object[]{2, "E"});
dt.Rows.Add(new object[]{2, "F"});
dt.Rows.Add(new object[]{3, "G"});
dt.Rows.Add(new object[]{3, "H"});
dt.Rows.Add(new object[]{3, "I"});
dt.Rows.Add(new object[]{4, "J"});
dt.Rows.Add(new object[]{4, "K"});
dt.Rows.Add(new object[]{4, "L"});

//1. method
List<datatable> result = dt.AsEnumerable()
		.GroupBy(x=>x.Field<int>("ID"))
		.Select(grp=>grp.CopyToDataTable())
		.ToList();


////2. method
//var result = dt.AsEnumerable()
//		.GroupBy(x=>x.Field<int>("ID"))
//		.ToList();
//
//List<datatable> dts = new List<datatable>();
//foreach(var d in result)
//{
//	dts.Add(d.CopyToDataTable());
//}</datatable></datatable></int></int></datatable>


"Invalid agument" error is easy to resolve. So, you have to debug your programme to find out where the error occur.
In the meanwhile, please read this: Invalid procedure call or argument (Error 5)[^]

[EDIT]
Raja Ganapathy wrote:

[in the comment to the solution1]
i go through your code sir.i have n numbers of columns i want to split first 24 columns and rows from the data table.How to do it.


If you want to get only few columns, replace:
C#
.Select(grp=>grp.CopyToDataTable())

with
C#
.Select(grp=>grp.Select(a=>new{ 
    Col1 = a.Field<Type>("Name1"),
    Col2 = a.Field<Type>("Name2"),
    ...
    ColN = a.Field<Type>("NameN")
    }).CopyToDataTable())

[/EDIT]
 
Share this answer
 
v2
C#
//this is datatable demo

DataTable  tbl = new DataTable();
tbl.Columns.Add("Column");
for(int i=0; i < 61; i++)
    tbl.Rows.Add(i.ToString());



DataTable[] splittedtables = tbl.AsEnumerable()
    .Select((row, index) => new { row, index })
    .GroupBy(x => x.index / 12)  // integer division, the fractional part is truncated
    .Select(g => g.Select(x => x.row).CopyToDataTable())
    .ToArray();
 
Share this answer
 
Comments
Maciej Los 19-Aug-16 2:31am    
In my opinion, that piece of code won't help. Please, read my answer.
Raja Ganapathy 19-Aug-16 3:07am    
i go through your code sir.i have n numbers of columns i want to split first 24 columns and rows from the data table.How to do it.
Maciej Los 19-Aug-16 3:14am    
Please, see updated answer (solution 2)
manishbg 19-Aug-16 3:09am    
DataTable[] splittedtables = tbl.AsEnumerable()
.Select((row, index) => new { row, index })
.GroupBy(x => x.index / 24) // integer division, the fractional part is truncated
.Select(g => g.Select(x => x.row).CopyToDataTable())
.ToArray();

use this
Raja Ganapathy 19-Aug-16 3:34am    
How to store the split columns and rows.
refer this example and split the columns to a separate tables inside dataset.

C#
DataTable dtMain = new DataTable();
       for (int i = 1; i <= 10; i++)
           dtMain.Columns.Add("col" + i);
       dtMain.Rows.Add(1);


       string[] columnNames = dtMain.Columns.OfType<DataColumn>().Select(k => k.ColumnName).ToArray();
       List<List<string>> lst = new List<List<string>>();


       int splitby = 3;

       int colCount = dtMain.Columns.Count;
       int splitCount = (colCount / splitby) + ((colCount % splitby) > 0 ? 1 : 0);

       for (int i = 0; i < splitCount; i++)
           lst.Add(columnNames.Skip(i * splitby).Take(splitby).ToList()); ;




       DataSet dsFinal = new DataSet();
       for (int i = 0; i < lst.Count; i++)
       {
           DataTable dtTemp = dtMain.Copy();
           dtTemp.TableName = "split" + i.ToString();
           columnNames.Except(lst[i]).ToList().ForEach(col => dtTemp.Columns.Remove(col));
           dtTemp.AcceptChanges();
           dsFinal.Tables.Add(dtTemp);

       }
 
Share this answer
 
Comments
Raja Ganapathy 19-Aug-16 8:35am    
Thank You to all.
C#
<pre lang="C#">DataTable tbl = new DataTable();
            tbl.Columns.Add("Column");
            for (int i = 0; i < 61; i++)
                tbl.Rows.Add(i.ToString());



            DataTable[] splittedtables = tbl.AsEnumerable()
                .Select((row, index) => new { row, index })
                .GroupBy(x => x.index / 12)  // integer division, the fractional part is truncated
                .Select(g => g.Select(x => x.row).CopyToDataTable())
                .ToArray();


DataTable dtarr1 = splittedtables[0];
 
Share this answer
 
v2
Comments
Richard Deeming 19-Aug-16 10:22am    
If you want to update your answer, click "Improve solution". DO NOT post your update as a new solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900