2013-09-27 55 views
0

我正在使用C#。我有兩個數據表,我想找到第一個數據表的行到第二個數據表。 示例。 首先數據表中的數據:如何查找數據表的rowindex到另一個數據表?

1 inam 
2 sohan 

二數據表的數據:

3 ranjan 
1 inam 
2 sohan 

現在我想知道的第一個數據表的前兩行的索引到第二個數據表。

請幫幫忙。 任何答案或建議

問候

+2

您應該考慮標記這個故事與數據訪問和您計劃使用的數據庫技術。 – neontapir

+0

爲什麼不使用for循環? – Panzercrisis

回答

1

您可以使用下面返回一個「子序列」的第一個索引擴展方法:

// I've used String.Join to get something that is comparable easily 
// from the ItemArray that is the object-array of all fields 
IEnumerable<string> first = table1.AsEnumerable() 
    .Select(r => string.Join(",",r.ItemArray)); // 
IEnumerable<string> second = table2.AsEnumerable() 
    .Select(r => string.Join(",", r.ItemArray)); 

int index = second.IndexOfSequence(first, null); // 1 

這裏延伸:

public static int IndexOfSequence<TSource>(this IEnumerable<TSource> input, IEnumerable<TSource> sequence, IEqualityComparer<TSource> comparer) 
{ 
    if (input == null) throw new ArgumentNullException("input"); 
    if (sequence == null) throw new ArgumentNullException("sequence"); 
    if (!sequence.Any()) throw new ArgumentException("Sequence must not be empty", "sequence"); 

    if (comparer == null) 
    { 
     comparer = EqualityComparer<TSource>.Default; 
    } 
    int index = -1; 
    int firstIndex = -1; 
    bool found = false; 
    TSource first = sequence.First(); 

    using (IEnumerator<TSource> enumerator = input.GetEnumerator()) 
    { 
     using (IEnumerator<TSource> enumerator2 = sequence.GetEnumerator()) 
     { 
      enumerator2.MoveNext(); 
      while (enumerator.MoveNext()) 
      { 
       index++; 
       found = comparer.Equals(enumerator.Current, enumerator2.Current); 
       if (found && firstIndex == -1) firstIndex = index; 
       if (found && !enumerator2.MoveNext()) 
        return firstIndex; 
      } 
     } 
    } 
    return -1; 
} 

用此樣本數據測試:

var table1 = new DataTable(); 
table1.Columns.Add("ID", typeof(int)); 
table1.Columns.Add("Name"); 
var table2 = table1.Clone(); 

table1.Rows.Add(1, "inam"); 
table1.Rows.Add(2, "Sohan"); 

table2.Rows.Add(3, "ranjan"); 
table2.Rows.Add(1, "inam"); 
table2.Rows.Add(2, "Sohan"); 
0

如果你沒有太多的量,這可能工作....

var tableOneIndex = -1; 
var tableTwoIndex = -1; 

foreach (var tableOneRow in tableOne.Rows) 
{ 
    tableOneIndex++; 

    foreach (var tableTwoRow in tableTwo.Rows) 
    { 
     tableTwoIndex++; 

     if (tableOneRow["name"].ToString() == tableTwoRow["name"].ToString()) 
     { 
      // Do whatever you wanted to do with the index values 
     } 
    } 
} 
+0

名稱不是表格中的唯一列。所有行也必須以相同的順序具有相同的字段,而不僅僅是一個。 –

+0

@TimSchmelter我沒有看到OP中的任何需求問題...... –

+0

_「現在我想知道第一個數據表的前兩行索引到第二個數據表中。」_因此,OP想要找到第一個索引在第二個表中,第一個表中的所有行都具有相同的字段。所以如果第二個表有100行,第一個表有10行,這十行必須是大表的一部分,而OP需要第一個索引。看看他(小)的例子。 –

0

作爲一個簡單的解決方案,這應該足夠了:

// Create and populate data tables 
DataTable dataTable1 = new DataTable(); 
dataTable1.Columns.Add("Name", typeof(string)); 

DataRow row1 = dataTable1.NewRow(); 
row1["Name"] = "Inam"; 

DataRow row2 = dataTable1.NewRow(); 
row2["Name"] = "Sohan"; 

dataTable1.Rows.Add(row1); 
dataTable1.Rows.Add(row2); 

DataTable dataTable2 = new DataTable(); 
dataTable2.Columns.Add("Name", typeof(string)); 

DataRow row3 = dataTable2.NewRow(); 
row3["Name"] = "Ranjan"; 

DataRow row4 = dataTable2.NewRow(); 
row4["Name"] = "Inam"; 

DataRow row5 = dataTable2.NewRow(); 
row5["Name"] = "Sohan"; 

dataTable2.Rows.Add(row3); 
dataTable2.Rows.Add(row4); 
dataTable2.Rows.Add(row5); 

// Loop through rows in first table 
foreach (DataRow row in dataTable1.Rows) 
{ 
    int rowIndexInSecondTable = 0; 

    // Loop through rows in second table 
    for (int i = 0; i < dataTable2.Rows.Count; i++) 
    { 
     // Check if the column values are the same 
     if (row["Name"] == dataTable2.Rows[i]["Name"]) 
     { 
      // Set the current index and break to stop further processing 
      rowIndexInSecondTable = i; 
      break; 
     } 
    } 

    // The index of the row in the second table is now stored in the rowIndexInSecondTable variable, use it as needed, for example, writing to the console 
    Console.WriteLine("Row with name '" + row["Name"] + "' found at index " + rowIndexInSecondTable.ToString()); 
} 
+0

名稱不是表格中的唯一列。所有行也必須以相同的順序具有相同的字段,而不僅僅是一個。 –

+0

情況並非如此,無論字段數量或字段順序如何,這都可以工作。我使用列的名稱而不是列索引,我通過索引訪問的唯一東西是行。 – Sean

+0

但是一旦有一行具有相同的名稱,就會中斷。較小表中的所有行必須與較大表的一部分具有相同的字段。除此之外,你只是比較一個領域而不是全部。 –

相關問題