[英]Order of Execution of Condition in a Where clause
I have the following statement with an Or condition. 我有一个带有或条件的以下语句。 I am looking what would be the result when both conditions are true.
我正在寻找两个条件都成立时的结果。
var result = db.Result.FirstOrDefault(x=>(x.ID==50||x.ID==60)&&x.Name="XYZ");
In a case where I have a row in the result table where both the conditions are true, ie; 在结果表中有两个条件都成立的情况下,即;
x.ID=50
and x.ID=60
. x.ID=50
和x.ID=60
。 What would be the result of the query? 查询的结果是什么?
I have already tested to get the result in a test environment. 我已经测试过,可以在测试环境中得到结果。 But I wanted to make sure that the order of execution would always remain the same no matter what the size of the database is.
但是我想确保无论数据库大小如何,执行顺序都将始终保持不变。 As I have read that the where clause uses some sort of indexing for faster retrieval, what would be the course of execution of this statement.
如我所读,where子句使用某种索引来加快检索速度,这条语句的执行过程将是什么。
The provided query is just a sample and the name ID has nothing to do with the unique identified of a table. 提供的查询只是一个示例,名称ID与表的唯一标识无关。
Question 题
How would the check be performed on the database? 如何在数据库上执行检查? I would expect a result where it first checks if
ID ==50
and on failure check if ID==60
. 我希望得到的结果是,它首先检查
ID ==50
并在失败时检查ID==60
。 If this is my expected result, would the query given above achieve my task? 如果这是我的预期结果,上面给出的查询是否可以完成我的任务?
Update after answer 回答后更新
I find it necessary to give a more clearer example so that the question is more understandable. 我认为有必要举一个更清晰的例子,以便使问题更容易理解。 (If this update makes the existing answers invalid, I am really sorry)
(如果此更新使现有答案无效,对不起)
var result = db.result.firstordefault(x=>(x.foreignkeyid == someval|| foreignkeyid == 123)&& x.Name=="XYZ");
And my database sample 还有我的数据库样本
ID foreignkeyid Name
1 123 XYZ
2 somevalue XYZ
3 anothervalue XYZ
In this case when the query is executed the result would return the row with ID==1
, but I want row wiht ID==2
to be returned. 在这种情况下,执行查询时,结果将返回
ID==1
的行,但我希望返回ID==2
行。
Worst case attempt to achieve the result 最坏的情况下尝试达到结果
var result = new Result();
result =db.Result.firstordefault(x=>x.ID==somevalue&&x.name==xyz);
if(result==null)
var result = db.firstordefault(x=>x.ID ==123&& x.name==xyz);
Given this example of yours: 给出您的以下示例:
var result = db.result.firstordefault(x=>(x.foreignkeyid == someval|| foreignkeyid == 123)&& x.Name=="XYZ");
In which you want to prioritize the result where fk = someval
(fk: foreign key), you can do the following: 要在其中优先排序
fk = someval
(fk:外键)的结果,可以执行以下操作:
db.set.OrderBy(x => x != someval) // false actually comes before true
.ThenBy(x => x != val2ndPriority)
.FirstOrDefault(x => (x.fk == someval ||
x.fk == val2ndPriority ||
x.fk == leastPriorityVal) &&
x.Name == "XYZ");
If you have a lot of "prioritized fk values", or if they are unknown at compile time, you can do: 如果您有很多“优先fk值”,或者在编译时未知,则可以执行以下操作:
var orderedEnum = db.set.OrderBy(x => x.Id);
foreach (var fk in fksByPriority)
orderedEnum = orderedEnum.ThenBy(x => x != fk);
var result = orderedEnum.FirstOrDefault(x => fksByPriority.Contains(x.fk) &&
x.Name == "XYZ");
How I would prefer it to look like: 我希望它看起来像:
Another different approach would be to get all possibly-relevant records and then run similar logic outside of the DB (your db Linq queries normally run smartly right inside the db): 另一种不同的方法是获取所有可能相关的记录,然后在数据库外部运行类似的逻辑(您的数据库Linq查询通常在数据库内部正常运行):
var results = db.set.Where(x => x.Name == "XYZ" &&
fks.Contains(x.fk)).ToArray();
var highestPriorityResult =
results.OrderBy(x => fksByPriority.IndexOf(x.fk)).FirstOrDefault();
On a final note, I wish to say that your problem indicates a possibly flawed design. 最后,我想说您的问题表明设计可能存在缺陷。 I can't imagine why you'd have this filtering-with-priority-foreign-key issue.
我无法想象为什么会有这个优先级过滤的外键问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.