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


I've never used it myself, but maybe i4o - Indexed LINQ can speed things up ...



+1


a source


How much it returns fewer rows, a case sensitivity issue? (ie "as" is not case sensitive by default, "Contains" by default)



If you are working in the context of a LINQ to SQL database, use the System.Data.Linq.SqlClient.SqlMethods.Like command instead of String.Contains.

+1


a source


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


Dumb question, but is something needed for the .AsEnumerable () parameter? I'm still only worried about Linq2Sql, but usually I treat things as AsQueryable as long as possible to avoid the "load dataset, then immediately filter it again" scenario.

0


a source







All Articles