views:

4545

answers:

2

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
+2  A: 

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
tvanfosson
A: 

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;
Jacob Proffitt