2011-01-06 19 views
1

我使用C#處理ASP.NET, 我需要將兩個數據集中的數據與兩個數據集中的「ID」進行比較,然後添加所有匹配行添加到新數據集。比較兩個數據集,並將值放置在一個新的數據集中

+3

你有什麼試圖解決這個問題?你有什麼困難?還是你想讓我們給你一個完整的解決方案,沒有做過研究? – 2011-01-06 05:22:39

回答

1

在每個DataSet中的表之間設置DataRelation,然後使用GetChildRows方法查找可以添加到新DataSet或任何其他數據結構的匹配項。有關示例,請參見Introduction to DataRelation Objects

0

這可以通過交叉點來完成兩個DataTable

using System.Data; 

public static class DataTableExtensions 
{ 
    public static IEnumerable<DataRow> Intersect(this DataTable table, DataTable other) 
    { 
     return table.AsEnumerable().Intersect(other.AsEnumerable()); 
    } 

    public static IEnumerable<DataRow> Intersect(this DataTable table, DataTable other, IEqualityComparer<DataRow> comparer) 
    { 
     return table.AsEnumerable().Intersect(other.AsEnumerable(), comparer); 
    } 
} 

這是一個shameless port of an elegant solution :)

假設你有兩個數據集SET1和SET2你想比較。做到這一點

var newtable = set1.Tables[0].Intersect(set2.Tables[0]).CopyToDataTable(); 

發佈更多的細節,如果這不是你想要的。

+0

這有何幫助?如何相交定義行關係? – TalentTuner 2011-01-06 05:46:37