Answered by:
linq to datatable

Question
-
hi to all,
i have an linq query and i want to convert it to datatable datasource.
my code
var result = from tab in tmpDT.AsEnumerable() group tab by tab["grouptxt"] into groupDt select new { grouptxt = groupDt.Key, qty = groupDt.Sum((r) => decimal.Parse(r["quant"].ToString())), netval = groupDt.Sum((r) => decimal.Parse(r["value"].ToString())) };
Any example?
Thursday, January 10, 2013 2:05 PM
Answers
-
Here is another article with a class enabling you to convert IEnumerable to Datatable
- Proposed as answer by Jason Dot Wang Monday, January 14, 2013 7:08 AM
- Marked as answer by Jason Dot Wang Friday, January 18, 2013 8:18 AM
Thursday, January 10, 2013 3:15 PM -
May be this is what you are looking for ?
IEnumerable<string[]> result = from enrty in dict where enrty.Key < 8 select enrty.Value; DataTable t = new DataTable(); DataColumn c1 = new DataColumn("colName1", typeof(string)); DataColumn c2 = new DataColumn("colName2", typeof(string)); DataColumn c3 = new DataColumn("colName3", typeof(string)); t.Columns.Add(c1); t.Columns.Add(c2); t.Columns.Add(c3); t.Rows.Clear(); foreach (string[] item in result) { t.Rows.Add(item); }
You can download the entire code for free from : http://sdrv.ms/V1Baxq
Or may be this article is useful for you
for more projects please visit : http://wildclick.wordpress.com/codelib/
- Edited by Aarsh (MCTS) Friday, January 11, 2013 7:01 PM to add links
- Proposed as answer by Jason Dot Wang Monday, January 14, 2013 7:08 AM
- Marked as answer by Jason Dot Wang Friday, January 18, 2013 8:18 AM
Friday, January 11, 2013 6:58 PM -
See the code sample in this article. You can just copy the code from their directly and then just call "CopyToDataTable" on your result.
- Proposed as answer by Md.Ibrahim Monday, January 14, 2013 6:54 AM
- Marked as answer by Jason Dot Wang Friday, January 18, 2013 8:18 AM
Friday, January 11, 2013 7:15 PM
All replies
-
The problem is that when you use Select new it creates an IEnumerable preventing you from using the result as a datasource for datatables.
This is no simple conversion here is an MSDN article that describe a solution MSDN Article that may help you
Thursday, January 10, 2013 2:39 PM -
can i do the query with out use IEnumerable?
Thursday, January 10, 2013 2:51 PM -
can i do the query with out use IEnumerable?
Hi,
to query using LINQ your object should implement IEnumerable or you should convert it as you done in your example code.
One good question is equivalent to ten best answers.
Thursday, January 10, 2013 2:57 PM -
Here is another article with a class enabling you to convert IEnumerable to Datatable
- Proposed as answer by Jason Dot Wang Monday, January 14, 2013 7:08 AM
- Marked as answer by Jason Dot Wang Friday, January 18, 2013 8:18 AM
Thursday, January 10, 2013 3:15 PM -
May be this is what you are looking for ?
IEnumerable<string[]> result = from enrty in dict where enrty.Key < 8 select enrty.Value; DataTable t = new DataTable(); DataColumn c1 = new DataColumn("colName1", typeof(string)); DataColumn c2 = new DataColumn("colName2", typeof(string)); DataColumn c3 = new DataColumn("colName3", typeof(string)); t.Columns.Add(c1); t.Columns.Add(c2); t.Columns.Add(c3); t.Rows.Clear(); foreach (string[] item in result) { t.Rows.Add(item); }
You can download the entire code for free from : http://sdrv.ms/V1Baxq
Or may be this article is useful for you
for more projects please visit : http://wildclick.wordpress.com/codelib/
- Edited by Aarsh (MCTS) Friday, January 11, 2013 7:01 PM to add links
- Proposed as answer by Jason Dot Wang Monday, January 14, 2013 7:08 AM
- Marked as answer by Jason Dot Wang Friday, January 18, 2013 8:18 AM
Friday, January 11, 2013 6:58 PM -
See the code sample in this article. You can just copy the code from their directly and then just call "CopyToDataTable" on your result.
- Proposed as answer by Md.Ibrahim Monday, January 14, 2013 6:54 AM
- Marked as answer by Jason Dot Wang Friday, January 18, 2013 8:18 AM
Friday, January 11, 2013 7:15 PM