通过 LINQ 从 DataTable 到 DataTable 的多个 on 子句
本文关键字:DataTable on 子句 LINQ 通过 | 更新日期: 2023-09-27 18:31:48
>我有一个关于 LINQ 的问题有两个数据表我要内部连接,代码如下:
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Columns.Add("TEST1");
dt1.Columns.Add("TEST2");
dt1.Columns.Add("TEST3");
dt1.Columns.Add("TEST4");
dt2.Columns.Add("TEST2");
dt2.Columns.Add("TEST5");
dt2.Columns.Add("TEST6");
dt2.Columns.Add("TEST7");
for (int i = 0; i < 10; i++)
{
DataRow row = dt1.NewRow();
row["TEST1"] = "aaa";
row["TEST2"] = string.Format("bbb-{0}{1}{2}", i, (i % 2 == 0 ? "-" : ""), (i % 2 == 0 ? "ddd" : ""));
row["TEST3"] = i.ToString();
row["TEST4"] = "ddd";
dt1.Rows.Add(row);
}
for (int i = 0; i < 8; i++)
{
DataRow row = dt2.NewRow();
row["TEST2"] = "bbb";
row["TEST5"] = i.ToString();
row["TEST6"] = i % 2 == 0 ? "ddd" : "";
row["TEST7"] = "sss";
dt2.Rows.Add(row);
}
dt1 的描述是:
TEST1 TEST2 TEST3 TEST4
---------------------------
aaa bbb-0-ddd 0 ddd
aaa bbb-1 1 ddd
aaa bbb-2-ddd 2 ddd
aaa bbb-3 3 ddd
aaa bbb-4-ddd 4 ddd
aaa bbb-5 5 ddd
aaa bbb-6-ddd 6 ddd
aaa bbb-7 7 ddd
aaa bbb-8-ddd 8 ddd
aaa bbb-9 9 ddd
dt2的描述是:
TEST1 TEST5 TEST6 TEST7
----------------------------
bbb 0 ddd sss
bbb 1 sss
bbb 2 ddd sss
bbb 3 sss
bbb 4 ddd sss
bbb 5 sss
bbb 6 ddd sss
bbb 7 sss
第一个问题是,我想将 dt1 和 dt2 连接
为 TEST2 TEST3 TEST7
---------------------------
bbb-0-ddd 0 sss
bbb-1 1 sss
bbb-2-ddd 2 sss
bbb-3 3 sss
bbb-4-ddd 4 sss
bbb-5 5 sss
bbb-6-ddd 6 sss
bbb-7 7 sss
所以我将代码编写为下面的状态,我想将 dt1 和 dt2 连接到 dt
DataTable dt = new DataTable();
dt.Columns.Add("TEST2");
dt.Columns.Add("TEST3");
dt.Columns.Add("TEST7");
var result = from A1 in dt1.AsEnumerable()
join
A2 in dt2.AsEnumerable()
on
A1.Field<string>("TEST2") equals (A2.Field<string>("TEST2") + "-" + A2.Field<string>("TEST5") + (A2.Field<string>("TEST6") == string.Empty ? "-" : string.Empty) + A2.Field<string>("TEST6"))
select new dt.LoadDataRow(new object[] { A1.Field<string>("TEST2"), A1.Field<string>("TEST3"), A2.Field<string>("TEST7") }, false);
result.CopyToDataTable();
可是行不通~此外,我想更改TEST3和TEST7的列,TEST3将被命名为X1,TEST3将被命名为X5,因此最新行的代码应该是
选择"新建 DT"。LoadDataRow(new object[] { A1.字段("TEST2"),X1=A1。字段("TEST3"), X5=A2.字段("TEST7") }, 假);
这是另一个问题。我该怎么办????
而不是 LoadDataRow
+ CopyToDataTable
方法,我总是更喜欢一个简单的foreach
,我在查询中添加这些行。这更清楚,不依赖于副作用。
您可以联接字符串并将两行都放在匿名类型中:
var query = from r1 in dt1.AsEnumerable()
join r2 in dt2.AsEnumerable()
on r1.Field<string>("TEST2")
equals string.Format("{0}-{1}-{2}"
, r2.Field<string>("TEST2")
, r2.Field<string>("TEST5")
, r2.Field<string>("TEST6"))
select new { r1, r2 };
foreach (var bothRows in query)
{
DataRow addedRow = dt.Rows.Add();
addedRow.SetField("TEST2", bothRows.r1.Field<string>("TEST2"));
addedRow.SetField("TEST3", bothRows.r2.Field<string>("TEST3"));
addedRow.SetField("TEST7", bothRows.r2.Field<string>("TEST7"));
}
在互联网上,您可以找到一个名为LinqToDataTable的非常有用的功能(附在下面),使用它,您可以将任何查询LINQ转换为DataTable,保留查询产生的数据类型。
public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
PropertyInfo[] oProps = null;
if (varlist == null) return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
(rec, null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}
应该很容易调用函数:
var result = (from A1 in dt1.AsEnumerable()
join
A2 in dt2.AsEnumerable()
on
A1.Field<string>("TEST2") equals (A2.Field<string>("TEST2") + "-" + A2.Field<string>("TEST5") + (A2.Field<string>("TEST6") == string.Empty ? "-" : string.Empty) + A2.Field<string>("TEST6"))
select new {
TEST2 = A1.Field<string>("TEST2"),
x1 = A1.Field<string>("TEST3"),
x2 = A2.Field<string>("TEST7")
});
DataTable dtFinal = LINQToDataTable(result);