0

I am developing an web application and using mvc, entity framework, ado.net entity. I want to know that which one is fast to fetch the data with some conditions : 1. Lambda Expression 2. Linq query

code snipped is below (in below code _dict is dictionary)

string fname = "", username = "", lname = "", mail = "";
            if (_dict.ContainsKey("fname"))
                fname = _dict["fname"].ToLower();
            if (_dict.ContainsKey("username"))
                username = _dict["username"].ToLower();
            if (_dict.ContainsKey("lname"))
                lname=_dict["lname"].ToLower();
            if (_dict.ContainsKey("mail"))
                mail = _dict["mail"].ToLower();

            var _admins = db.AdminsTables.Where(x =>
                x.firstname.ToLower().Contains(fname) &&
                x.username.ToLower().Contains(username) &&
                x.lastname.ToLower().Contains(lname) &&
                x.useremail.ToLower().Contains(mail)).ToList();

OR

string fname = "", username = "", lname = "", mail = "";
            if (_dict.ContainsKey("fname"))
                fname = _dict["fname"].ToLower();
            if (_dict.ContainsKey("username"))
                username = _dict["username"].ToLower();
            if (_dict.ContainsKey("lname"))
                lname=_dict["lname"].ToLower();
            if (_dict.ContainsKey("mail"))
                mail = _dict["mail"].ToLower();

                      var _admins = (from record in db.AdminsTables
                           where record.firstname.ToLower().Contains(fname) && record.username.ToLower().Contains(username) && record.lastname.ToLower().Contains(lname) && record.useremail.ToLower().Contains(mail)
                           orderby record.id descending
                           select record).ToList();

Please suggest me which one is faster and is possible then give reason also.

developer10214
  • 1,128
  • 1
  • 11
  • 24
Karan Prince
  • 277
  • 1
  • 3
  • 8
  • I think they produce same sql query. but in second case you are also sorting.. – karaxuna Nov 01 '13 at 06:58
  • @karaxuna yes both are giving same results but my question is which is fast way from these two ? – Karan Prince Nov 01 '13 at 07:29
  • possible duplicate of [LINQ - Fluent and Query Expression - Is there any benefit(s) of one over other?](http://stackoverflow.com/questions/214500/linq-fluent-and-query-expression-is-there-any-benefits-of-one-over-other) – assuming that the difference by sorting is obvious. – Gert Arnold Nov 01 '13 at 13:48

1 Answers1

0

In fact you can turn nearly every linq query into a lambda expression. When you working with linq-to-entities or entity-frame work, it doesn't matters if use linq querys or lambda expressions. What matters is the resulting SQL query. So in your case the first code sample would be faster, because (like @karaxuna already mentioned) you added sorting to the query. When performance is your focus, you should compare the linq query in code and a sql query in the database. For example:

  1. write a SQL query for the result you want optimize it and look how long it takes
  2. write a LINQ query in code for the same result, look how long it takes

If the SQL query is much faster, you should create a view, a stored procedure and call it from code. If the measured times are nearly the same, it's up to you if use the LINQ query or create a view, a stored procedure and call it from code. If the LINQ query is much faster, you should look at the generated SQL query it seems to better than your query.

As far as I experienced, there is only a significant performance difference when you query data which are distributed over multiple tables and need a lot of joins and/or sorting operations.

developer10214
  • 1,128
  • 1
  • 11
  • 24