LINQ-联接2个数据表-通配符 [英] LINQ - join 2 datatables - wildcard

查看:142
本文介绍了LINQ-联接2个数据表-通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LINQ联接2个数据表.我试图只从dtTable2取回1个字段,从dtTable1取回所有字段.执行此代码时,我有两列.第一列包含dtTable2.field2中的值,第二列包含值"System.Data.DataRow".我知道,如果我在dtTable1中明确指定列,则将获得所需的结果.是否可以指定通配符以从dtTable1获取所有列?

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

推荐答案

否.您需要列出所需的属性,或从行对象中引用属性.请注意,如果这是强类型的行,则可以将其强制转换为类型,然后直接在代码中使用这些字段.

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

这篇关于LINQ-联接2个数据表-通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆