?
SolvedPrivate

Load LINQ query into datatable

Posted on 2014-02-27
6
Medium Priority
?
44 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

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
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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