LINQ and DataTable.Select - How to get the same results?
I'm trying to rewrite the direct (disabled) DataSet.DataTable.Select to LINQ for text search:
string search = "ProductNo like '%" + searchpattern +
"%' OR ProductName like '%" + searchpattern +
"%' OR Description like '%" + searchpattern + "%'";
DataSetProducts.sk_productsRow[] dsp = (DataSetProducts.sk_productsRow[])dsProducts.sk_products.Select(search, sort);
It works really fast. However, if I repeat it in LINQ:
productlist = from prds in dsProducts.sk_products.AsEnumerable()
where (prds.Field<string>("productno").Contains(searchpattern) ||
prds.Field<string>("productname").Contains(searchpattern) ||
prds.Field<string>("description").Contains(searchpattern))
select prds;
It gets very slow due to text operations and returns much fewer lines. How can I achieve the same results at the same speed? I only have 1000 records in the table.
0
a source to share
4 answers
The reason you see the difference in speed when executing a LINQ query versus a disabled dataset is because the dataset is already in memory and the LINQ query is being executed at runtime.
Also, if you haven't installed the LINQ Visualizer , which will show you the query that is actually being executed.
0
a source to share