SolvedPrivate

Load LINQ query into datatable

Posted on 2014-02-27
6
37 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
  • 3
  • 2
6 Comments
 
LVL 69

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 62

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
3 Use Cases for Connected Systems

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

 
LVL 69

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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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)…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now