3

我有以下代码:

var deletedData
    = (from c in this.DataSet.Tables["TableName1"].AsEnumerable()
       from deletedData1 in this.DataSet.Tables["TableName2"].AsEnumerable()
       where (
           (c.Field<string>("ColumnName1")
            == deletedData1.Field<string>("ColumnName2"))
           && (c.Field<string>("ColumnName1") == someString))
       select new
           {
               T1 = c.Field<string>("ColumnName3"),
               T2 = deletedData1.Field<string>("ColumnName4"),
               T3 = c.Field<string>("ColumnName5"),
               T4 = deletedData1.Field<string>("ColumnName6")
           });

执行此操作后,当我打开结果deletedData显示功能评估超时。任何人都可以帮我摆脱这个吗? Table1有 18000 行,Table2有 400 行。当我使用deletedData1.

4

1 回答 1

2

不要用于WhereLinq-To-Objectbut中链接表/集合Join

var deletedData = from c in this.DataSet.Tables["TableName1"].AsEnumerable()
                  let col1 = c.Field<string>("ColumnName1")
                  join deletedData1 in this.DataSet.Tables["TableName2"].AsEnumerable()
                  on col1 equals deletedData1.Field<string>("ColumnName2")
                  where col1 == someString
                  select new
                  {
                       T1 = c.Field<string>("ColumnName3"),
                       T2 = deletedData1.Field<string>("ColumnName4"),
                       T3 = c.Field<string>("ColumnName5"),
                       T4 = deletedData1.Field<string>("ColumnName6")
                  };

为什么 LINQ JOIN 比使用 WHERE 链接快得多?

但是,由于这只是一个查询,您应该以某种方式实现它。因此,您可以在 a 中使用它foreach或使用ToList. 否则你总是在评估它。

于 2013-10-09T11:03:29.853 回答