3

I have the following code:

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")
           });

After executing this when I opened result of deletedData it's showing function evaluation timed out. Can any one help me out to get rid of this? Table1 has 18000 rows and Table2 has 400 rows. My UI is hanging when I use deletedData1.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Vinod kumar
  • 129
  • 5
  • 13

1 Answers1

2

Don't use Where to link tables/collections in Linq-To-Object but 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")
                  };

Why is LINQ JOIN so much faster than linking with WHERE?

However, since this is just a query you should materialize it somehow. So you could consume it in a foreach or create a collection with ToList. Otherwise you are evaluating it always.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thnq so much for the answer.why should not we use where to link tables. what does 'let' mean in the above LINQ.. – Vinod kumar Oct 09 '13 at 11:18
  • @user2862430: The [`let`](http://msdn.microsoft.com/en-us/library/bb383976.aspx) lets you store a variable in a query, so that you can reuse it later. That increases readability and performance (in Linq-To-Objects). You could also repeat the same (as you've done with `c.Field("ColumnName1")`). – Tim Schmelter Oct 09 '13 at 11:22
  • ThanQ so much..its working fine.. I want you to justify for above solution. – Vinod kumar Oct 09 '13 at 11:36
  • why don't we use where to link tables in linq to object – Vinod kumar Oct 09 '13 at 11:54