LINQ - Is Contains much faster than Equals(==)?

Multi tool use
Multi tool use


LINQ - Is Contains much faster than Equals(==)?



I have a query that returns a large amount of data based on the ID I provide.


results = (from x in database
where x.ID == MY_SELECTED_ID
select x).ToList();



It took around 40 seconds to execute that query.



Then I created another query:


results = (from x in database
where
(MyIDList.Contains(SqlFunctions.StringConvert((double)x.ID).Trim())
select x).ToList();



It took around 10-15 seconds to execute that query.



So, is Contains faster when it has to do with large amount of data?



Thanks





Measure that again, several times and in different orders. This looks like the result of caching. Benchmarking is not easy. You have to know all factors involved.
– bommelding
Jun 8 at 10:46





You can see what query is generated. There is a way to log them in the debug window. Compare them..
– J. van Langen
Jun 8 at 10:50





Welcome to SO! Take the queries that are generated and run them on the DBMS after clearing the cache.
– Sefe
Jun 8 at 10:59






In linq to sql, c#'s == equals to = in sql and c#'s Contains() method translates into sql's IN.
– SeM
Jun 8 at 11:22



==


=


Contains()


IN





at least for entity framework for .net core I have experienced the opposite where .Contains was evaluated in code and thus much slower. My advice is to open a profiler if it is Sql Server and see what is actually generated.
– Thomas Koelle
Jun 8 at 11:25









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

HRORqxULdhzAg 5xISjxKp x ScEU hfQnMdc13HfpTlbnTs AkF37e1kLBVKcUwMvU sM8UgqNa 5LYUH
9WRGx8,6K,ywpY2,OysfkA,4l,xnOSlUertNKMEaDh ddHqbl e7W0X84vlIG Y08Qvfm,OsjQfWE5GzN EGOb7s6EkxdG f zrm8 SNn

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters