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
SolvedPrivate

Load LINQ query into datatable

Posted on 2014-02-27
6
39 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 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What are the simplest ways to create a Merge Sort? 3 29
LINQ Select all records by MAX data syntax help 1 18
Name Space error VS2015 1 25
abstract class C# 1 31
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)…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 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