using Linq with multiple where conditions

这一生的挚爱 提交于 2019-12-10 12:32:08

问题


I have a class representing a table on database that is defined:

public class MyClass{
    public int MyClassId{get;set;}
    public string Name{get;set;} 
    public string LastNamw{get;set;}
    public DateTime From{get;set;}
    public DateTime To{get;set;}
}

On which I want to run some search query against oracle database.

Now the question is :

var list = context.MyClass
    .Where(x => x.From>= FromMyDate)
    .Where(x => x.To <= ToMyDate);


var list = context.MyClass
    .Where(x => x.From>= FromMyDate && x.To <= ToMyDate);

Is it better to use multiple rows with where condition or one? And why... please

As far as I understand, I use multiple where clauses when I want to search on result of the first condition.


回答1:


It shouldn't matter

You are just creating an Expression tree that is passed to the Oracle Provider and it is the provider's job to create the resultant SQL query. Even though the Expression tree would be slightly different between the two, it should still result in the same sql. I like to use a sql profiler to make sure my linq queries are producing efficient sql.




回答2:


I will be following John Skeet answer as posted by Vossad01.

Proper Linq where clauses



来源:https://stackoverflow.com/questions/11314203/using-linq-with-multiple-where-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!