C# 使用LINQ查询数据表的另一个帮助

C# 使用LINQ查询数据表的另一个帮助,c#,linq,C#,Linq,我有同样的问题,因为我仍然使用相同的样本 DataTable table = new DataTable(); table.Columns.Add("column1", typeof(int)); table.Columns.Add("column2", typeof(int)); table.Columns.Add("column3", typeof(string)); table.Rows.Add(1, 0, "a"); table.Rows.

我有同样的问题,因为我仍然使用相同的样本

    DataTable table = new DataTable();
    table.Columns.Add("column1", typeof(int));
    table.Columns.Add("column2", typeof(int));
    table.Columns.Add("column3", typeof(string));
    table.Rows.Add(1, 0, "a");
    table.Rows.Add(2, 1, "b");
    table.Rows.Add(3, 1, "c");
    table.Rows.Add(4, 3, "d");
    table.Rows.Add(5, 3, "e");
我这样做是为了返回第3列的值,第1列中的值也出现在第2列中:

var x = (from t1 in table.AsEnumerable()
         select t1.Field<int>(0)).Intersect
        ((from t2 in table.AsEnumerable()
         select t2.Field<int>(1)).Distinct());
var x=(来自表中的t1.AsEnumerable()
选择t1。字段(0))。相交
((来自表中的t2.AsEnumerable()
选择t2.Field(1)).Distinct();
如何根据上面的LINQ获得column3的值

我可以使用公认的答案:

var result=(来自表中的t1.AsEnumerable()
在t1.Field(0)上的表.AsEnumerable()中连接t2等于t2.Field(1)

虽然我认为我不能在第一个LINQ的反面使用它,它可以翻译为“返回column3的值,其column1中的值不会出现在column2中”。但是,我可以在
Intersect()上将我上面的LINQ更改为
Except()
。我只需要知道如何使用
var x
获取行集合或列3的值。

这些应该可以满足您的需要:

// collection of datarows
var rows = table.AsEnumerable().Where(i => i.Field<int>(0) == i.Field<int>(1));

// collection of strings (field 3 of rows)
var rowvalues = rows.Select(j => j.Field<string>(3));
//数据行的集合
var rows=table.AsEnumerable()。其中(i=>i.Field(0)==i.Field(1));
//字符串集合(第3行字段)
var rowvalues=rows.Select(j=>j.Field(3));

为什么要使用
数据表
?强类型集合通常要好得多。
// collection of datarows
var rows = table.AsEnumerable().Where(i => i.Field<int>(0) == i.Field<int>(1));

// collection of strings (field 3 of rows)
var rowvalues = rows.Select(j => j.Field<string>(3));