locked
where clause can be put twoice in linq , isthere any reason behind it. RRS feed

  • Question

  • User2102072086 posted
    hi,
    in linq, where clause can be put twoice , pls give an example where two where clause is required.
     
    one more thing in method based syntex , if i put two where clauses then is the execution follows any order. like
    left to first or it can execute anywhere clause in any order. 
     
    yours sincerley 
    Sunday, March 1, 2020 12:43 PM

Answers

  • User-821857111 posted

    well that means it does not follow left to right execution if two where clause is applied?

    In LINQ to SQL, two Where clauses will be combined into one WHERE clause in SQL using the AND operator on the specified filter. You obviously can't have more than one WHERE clause in SQL.  Logically, that is the same as chaining multiple Where clauses in LINQ and having them execute from left to right, with each clause filtering the result of the previous clause.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 4, 2020 7:51 AM
  • User-821857111 posted

    It works in the same way as having all filters in one Where clause separated with &&. Each filter further refines the result set.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 5, 2020 9:35 AM

All replies

  • User1120430333 posted

    One can do anything he or she wants to do concerning this. Is valid and a practical usage? I would say no. Just becuase one can doesn't mean that one should.. 

    Sunday, March 1, 2020 6:12 PM
  • User-17257777 posted

    Hi rajemessage,

    You can use multiple where clause based on the needs, you can chain together all the LINQ methods you want or use them separately

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/where-clause

    https://stackoverflow.com/questions/679644/multiple-where-clause-in-linq

    Best Regards,

    Jiadong Meng

    Monday, March 2, 2020 6:54 AM
  • User2102072086 posted

    well that means it does not follow left to right execution if two where clause is applied?

    Wednesday, March 4, 2020 5:44 AM
  • User-17257777 posted

    Hi rajemessage,

    Yes, any decent query optimizer will look at all the parts of the WHERE clause and figure out the most efficient way to satisfy that query.

    You can refer to this link: https://stackoverflow.com/a/11436528

    Best Regards,

    Jiadong Meng

    Wednesday, March 4, 2020 6:12 AM
  • User-821857111 posted

    well that means it does not follow left to right execution if two where clause is applied?

    In LINQ to SQL, two Where clauses will be combined into one WHERE clause in SQL using the AND operator on the specified filter. You obviously can't have more than one WHERE clause in SQL.  Logically, that is the same as chaining multiple Where clauses in LINQ and having them execute from left to right, with each clause filtering the result of the previous clause.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 4, 2020 7:51 AM
  • User2102072086 posted

    i am working on linq to object

    Thursday, March 5, 2020 5:14 AM
  • User-821857111 posted

    It works in the same way as having all filters in one Where clause separated with &&. Each filter further refines the result set.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 5, 2020 9:35 AM