C# 两个数据表需要根据一个数据表的where条件进行连接
我有两个数据表 1,它运行查询并存储值C# 两个数据表需要根据一个数据表的where条件进行连接,c#,csv,datatable,C#,Csv,Datatable,我有两个数据表 1,它运行查询并存储值 Select customer, address,**zipcode** from sometable where zipcode = ? mycmd.Parameters.Add("@zipcode", OdbcType.VarChar).Value = {**how to bring the value from datatable 2 zip**; 进入数据表 2将csv文件读入数据表zip、lat、long 我正在对来自datatable 1和
Select customer, address,**zipcode** from sometable where zipcode = ?
mycmd.Parameters.Add("@zipcode", OdbcType.VarChar).Value = {**how to bring the value from datatable 2 zip**;
进入数据表
2将csv文件读入数据表zip、lat、long
我正在对来自datatable 1和datarows的datarows运行foreach循环
如果在where条件中找到与zip值匹配的行,那么我希望包含datatable 2中的lat和long值
我的最终结果将看起来像客户、地址、zipcode、lat、long
我在网上搜索了好几个网站,但没有找到任何东西
我不能在这里发布两个数据表代码,可以吗?这解决了我的问题
public static DataTable MergeTwoDataTables(DataTable dt1, DataTable dt2)
{
var collection = from t1 in dt1.AsEnumerable()
join t2 in dt2.AsEnumerable() on ToNumericOnly(t1["ZIPCODE"].ToString().Trim().Substring(0,5))equals t2["Zip"] into DataGroup
from item in DataGroup.DefaultIfEmpty()
select new
{
CUSTOMER = ToNumericOnly(t1["CUSTOMER"].ToString()),
ADDRESS=t1["ADDRESS"],
ZIPCODE = t1["ZIPCODE"].ToString().Trim(),
LAT= item == null ? string.Empty : item["lat"],
LONG= item == null ? string.Empty : item["long"]
};
DataTable result = new DataTable("CustomerInfo");
result.Columns.Add("CUSTOMER", typeof(string));
result.Columns.Add("ADDRESS", typeof(string));
result.Columns.Add("ZIPCODE", typeof(string));
result.Columns.Add("LAT", typeof(string));
result.Columns.Add("LONG", typeof(string));
foreach (var item in collection)
{
result.Rows.Add(item.CUSTOMER,item.ADDRESS,item.ZIPCODE,
item.LAT,item.LONG);
Console.WriteLine("{0}", item);
//Console.ReadLine();
}
return result;
}
不确定这是否有帮助我不能在这里发布两个数据表代码,可以吗?-大概您可以添加表的名称和它们拥有的列。我假设其中一个叫做zipcode,另一个叫做sometable,但你也可以给它取个真名。我假设您想要进行联接,但是由于两个输入表的结构不清楚,目前无法告诉您哪一列对它们都是公共的。你能添加必要的额外细节吗?