C# 使用.Select().Join()连接两个数据表
我一直在到处寻找,但毫无结果。我有两个数据表,我想在不创建新的结果表的情况下连接它们,因为我只需要更新其中一个表中的一些行以显示在网格视图中,类似于下面的代码,但需要连接:C# 使用.Select().Join()连接两个数据表,c#,linq,join,datatable,C#,Linq,Join,Datatable,我一直在到处寻找,但毫无结果。我有两个数据表,我想在不创建新的结果表的情况下连接它们,因为我只需要更新其中一个表中的一些行以显示在网格视图中,类似于下面的代码,但需要连接: sage_invoices.Select("CCE2 IS NULL") .ToList<DataRow>() .ForEach(row => { row["Error"]
sage_invoices.Select("CCE2 IS NULL")
.ToList<DataRow>()
.ForEach(row =>
{
row["Error"] = 1;
row["ErrorMessage"] = "Missing Region Code (Dimension 2 - CCE2)";
});
我找不到的是如何使用连接两个数据表。连接:
sage_invoices.Select()
.Join(<What Goes here?>)
.ToList<DataRow>()
.ForEach(row =>
{
row["Error"] = 1;
row["ErrorMessage"] = "ITMREF is not a Sage Product Code";
});
sage_发票。选择()
.Join()
托利斯先生()
.ForEach(行=>
{
行[“错误”]=1;
行[“ErrorMessage”]=“ITMREF不是Sage产品代码”;
});
如果有人能给我指出正确的方向,我将不胜感激
谢谢
Gareth我通常通过构建一个匿名对象来实现这一点,该匿名对象通过连接或GroupJoin包含对我的源对象和目标对象的引用,然后循环连接的结果以更新我的目标对象。请参见下面的示例 请查看和上的文档。Join非常适合1-1匹配,而GroupJoin是0-*匹配(类似于SQL左连接)。Join和GroupJoin参数允许您为每个IEnumerable指定一个选择器函数,然后为输出对象指定一个选择器函数。请注意,下面的
t1
和t2
参考表1
和表2
using System;
using System.Data;
using System.Linq;
public class Program
{
public static void Main()
{
var table1 = GetEmptyTable();
table1.Rows.Add(1, "Old Value", false);
table1.Rows.Add(2, "Untouched Value", false);
var table2 = GetEmptyTable();
table2.Rows.Add(1, "New Value", false);
table2.Rows.Add(3, "Unused Value", false);
Console.WriteLine("Before...");
Console.WriteLine(PrintTable(table1));
var matched = table1.Select()
.Join(table2.Select(), t1 => (int)t1["A"], t2 => (int)t2["A"], (t1, t2)
=> new
{
DestinationRow = t1,
SourceRow = t2
});
foreach (var match in matched)
{
match.DestinationRow["B"] = match.SourceRow["B"];
match.DestinationRow["C"] = true;
}
Console.WriteLine("After...");
Console.WriteLine(PrintTable(table1));
}
private static DataTable GetEmptyTable()
{
var table = new DataTable();
table.Columns.Add("A", typeof(int));
table.Columns.Add("B", typeof(string));
table.Columns.Add("C", typeof(bool));
return table;
}
private static string PrintTable(DataTable table)
{
return string.Join(Environment.NewLine, table.Select().Select(x => "[" +
string.Join(", ", x.ItemArray) + "]"));
}
}
在VS中的帮助也不好,因为它把你送到这里:谢谢泰勒,我想我已经明白了。我现在有下面的代码,但它没有返回任何东西,即使我知道它应该返回(在MSSQL中测试)。“src”中的一行的ITMREF值为10,而“lookup”中的一行的BillingCode值为10。这两列都是varchar/string。var error_rows=sage_invoices.Select().Join(sage_billing_conversion.Select(),src=>sage_invoices.Columns[“ITMREF”],lookup=>sage_billing_conversion.Columns[“BillingCode”],(src,lookup)=>new{DestinationRow=src,SourceRow=lookup});看起来您的键选择器功能正在使用
sage\u发票
和sage\u账单
转换,而它们应该使用src
和查找
src
和lookup
是单独的DataRow
实例,因此您本质上提供了一个映射函数来检索每个DataRow
的键值sage_发票。列
和sage_账单_转换。列
正在返回一个DataColumn
实例(不是实际值),这两个表之间永远不会匹配。哦,天哪。。。我显然需要咖啡。谢谢泰勒,明白了。你是个传奇人物!令人惊叹的很高兴我能帮忙。
using System;
using System.Data;
using System.Linq;
public class Program
{
public static void Main()
{
var table1 = GetEmptyTable();
table1.Rows.Add(1, "Old Value", false);
table1.Rows.Add(2, "Untouched Value", false);
var table2 = GetEmptyTable();
table2.Rows.Add(1, "New Value", false);
table2.Rows.Add(3, "Unused Value", false);
Console.WriteLine("Before...");
Console.WriteLine(PrintTable(table1));
var matched = table1.Select()
.Join(table2.Select(), t1 => (int)t1["A"], t2 => (int)t2["A"], (t1, t2)
=> new
{
DestinationRow = t1,
SourceRow = t2
});
foreach (var match in matched)
{
match.DestinationRow["B"] = match.SourceRow["B"];
match.DestinationRow["C"] = true;
}
Console.WriteLine("After...");
Console.WriteLine(PrintTable(table1));
}
private static DataTable GetEmptyTable()
{
var table = new DataTable();
table.Columns.Add("A", typeof(int));
table.Columns.Add("B", typeof(string));
table.Columns.Add("C", typeof(bool));
return table;
}
private static string PrintTable(DataTable table)
{
return string.Join(Environment.NewLine, table.Select().Select(x => "[" +
string.Join(", ", x.ItemArray) + "]"));
}
}