SolvedPrivate

Load LINQ query into datatable

Posted on 2014-02-27
6
42 Views
Last Modified: 2016-02-10
How would I load the results of this LINQ Query into a datatable?

        Dim Query = _
  (From Region In dataset1.Tables(0) _
  Select Region.Field(Of String)("Region")).Distinct()

Open in new window

0
Comment
Question by:lrbrister
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39892502
You need to use LoadDataRow. Check this example:

DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("IsDefault", typeof(bool));
dt.Columns.Add("DisplayOrder", typeof(int));
dt.Columns.Add("FullName", typeof(string), "Name + Iif(IsDefault, ' *', '')");

//The LoadDataRow method fills the DataTable declared above
var items = (from item in Items
      orderby item.DisplayOrder, item.Name
      select dt.LoadDataRow(new object[] { item.Id, item.Name, item.IsDefault, item.DisplayOrder }, false)).ToList();
0
 

Author Comment

by:lrbrister
ID: 39892539
Eric
That's in C# and I can't seem to get the conversion to work correctly
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39892573
Hi  lrbrister;

The following is a generic code, helper class to do what you want to do. Please see the following article.

How to: Implement CopyToDataTable<T> Where the Generic Type T Is Not a DataRow
0
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39892630
have you tried:

dim items = (from item in Items
      orderby item.DisplayOrder, item.Name
      select dt.LoadDataRow(new object() { item.Id, item.Name, item.IsDefault, item.DisplayOrder }, false)).ToList()
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 39892658
this is a working example in VB:

Public Class Form1

      Private Sub UltraButton1_Click(sender As Object, e As EventArgs) Handles UltraButton1.Click
            Dim dt As DataTable = New DataTable()
            dt.Columns.Add("Id", GetType(Integer))
            dt.Columns.Add("Name", GetType(String))


            '//The LoadDataRow method fills the DataTable declared above
            Dim Items As New List(Of ItemType)
            Items.Add(New ItemType() With {.Id = 1, .Name = "item 1"})
            Items.Add(New ItemType() With {.Id = 2, .Name = "item 2"})
            Items.Add(New ItemType() With {.Id = 3, .Name = "item 3"})
            Dim xx = (From item In Items Order By item.Name Select dt.LoadDataRow(New Object() {item.Id, item.Name}, False)).ToList()
      End Sub

End Class

Public Class ItemType

      Public Id As Integer
      Public Name As String

End Class
0
 

Author Closing Comment

by:lrbrister
ID: 39892940
Thanks for sticking with me
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

724 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