0

我正在使用 C#。我有两个数据表,我想将第一个数据表的行查找到第二个数据表中。例子。第一个数据表的数据:

1  inam
2  sohan

第二个数据表的数据:

3  ranjan
1  inam
2  sohan

现在我想知道第一个数据表的前两行到第二个数据表的索引。

请帮助各位。任何答案或建议

此致

4

3 回答 3

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");
于 2013-09-27T12:32:47.053 回答
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
        }
    }
}
于 2013-09-27T12:34:21.480 回答
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());
}
于 2013-09-27T12:35:51.467 回答