18670

LINQ - join 2 datatables - wildcard

Question:

I am using LINQ to join 2 datatables. I am trying to get back only 1 field from dtTable2 and all the fields from dtTable1. When this code executes, I have two columns. The first column contains the value from dtTable2.field2 and the second column has the value "System.Data.DataRow". I know that if I explicitly specify the columns in dtTable1, I will have the results that I need. Is it possible to specify a wildcard to get all the columns from dtTable1?

Dim results = _ From r In dtTable1.AsEnumerable _ Join c In dtTable2.AsEnumerable _ On c.Field1 Equals r.Field1 _ Select c.field2, r Datagridview.datasource = results.ToList

Answer1:

No. You'll need to list the properties that you want or reference the properties from the row object. Note that if this is a strongly-typed row you can cast it to the type and use the fields directly in your code.

Dim results = _ From r In dtTable1.AsEnumerable _ Join c In dtTable2.AsEnumerable _ On c.Field1 Equals r.Field1 _ Select c.field2, r.Field1, r.Field3, r.Field4 Datagridview.datasource = results.ToList

Answer2:

It's a trifle awkward, but you can do it with a List object and Concat. I'm not familiar enough with VB to give you the correct syntax, but here's the C#

var results = from r in dtTable1.AsEnumerable() join c in dtTable2.AsEnumerable() on c.Field1 equals r.Field1 select new List<object>(r.ItemArray).Concat(new List<object>() { c.Field2 }) datagridview.DataSource = results;

Recommend

  • Get what exists in one DataTable but not another using LINQ
  • CurrentDateTime() in EF
  • how to bind datatable to webgrid using MVC?
  • asp.net GridView IF empty show message
  • WCF web service returning json format data
  • How can I find the installation directory of a specific program?
  • ObjectMaterialize in EF not firing on first level query
  • MySQL wildcard replace
  • How java deals with references to a generic type
  • How to bind comma separated list of values to List
  • Importing Excel files with a large number of columns header into mysql with c#
  • Sum data table columns using linq
  • Stitching 2 images (OpenCV)
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Is there a way to disable a specific event in kendo ui scheduler?
  • Generic classes with Collection getter of other types
  • Double-click autofill - dynamic based on adjacent cell
  • Excel VBA URLDownloadToFile Error for https Ressource
  • Creating a Multidimensional, Associative Array in VBScript
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • Symfony 2 error page response
  • NHibernate manually control fetching
  • Which open source license has no forking [closed]
  • How to explicitly/implicitly implemented interface members in C++/CLI?
  • Conversion from string “a” to type 'Boolean' is not valid
  • Is playing sound in Javascript performance heavy?
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • Django: Count of Group Elements
  • Linq Objects Group By & Sum
  • Insert into database using onclick function
  • How to check if every primary key value is being referenced as foreign key in another table
  • Knitr HTML Loop - Some HTML output, some R output
  • How to handle AllServersUnavailable Exception
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • How to get next/previous record number?
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?