Vinod kumar
Vinod kumar

Reputation: 129

Function Evaluation is timed out while using LINQ in c#

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.

Upvotes: 3

Views: 6323

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460238

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.

Upvotes: 2

Related Questions