1

facts.UnderlyingDataTable is a DataTable


var queryResults4 = //get all facts
    (from f in facts.UnderlyingDataTable.AsEnumerable()
        where f.RowState != DataRowState.Deleted &&
            FactIDsToSelect.Contains(f.Field<int>("FactID"))
        select f);

var queryResults5 = (from f in queryResults4.AsEnumerable()
    orderby UF.Rnd.Next()
    select f);


return queryResults5.ToList();

The problem is this line queryResults5.ToList();

It returns a list of DataRows. But is super slow to do this.

I am happy to return any object that implements IEnumerable. What should I do? I seems the conversion from whatever the var is to List<DataRow> is slow.

Thanks for your time.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445

2 Answers2

2

First, not the ToList itself is slow but the query that gets executed in this method. So maybe your DataTable contains many rows. I assume also that FactIDsToSelect is large which makes the Contains check for every row slow .

You could use CopyToDataTable to create a new DataTable with the same schema instead of a List since that is more natural for an IEnumerable<DataRow>. However, as i have mentioned, that would not solve your performance issue.

You could optimize the query with a Join which is much more efficient:

var q =   from row in UnderlyingDataTable.AsEnumerable()
          where row.RowState != DataRowState.Deleted 
          join id in FactIDsToSelect
          on row.Field<int>("FactID") equals id
          select row;
var newTable = q.CopyToDataTable();

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

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Please try with following.

List<DataRow> list = new List<DataRow>(UnderlyingDataTable.Select("FactID = " + id.ToString(),DataViewRowState.Unchanged));

You may need to change the DataViewRowState argument in .Select method.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
mit
  • 1,763
  • 4
  • 16
  • 27