本文介绍了| LINQ中的操作员操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

|(pipe)运算符?我想知道operator(|)的操作.我想检查不同表中的多列.在我的情况下,只有一个表包含该垂直列的值.

|(pipe) operator in linq?i want to know operation of operator(|).i want to check multiple columns in different table.In my scenario only single table contain value for that perticular column.

我的代码:

&& swr.Date_Of_Event.Date < DateTime.Today.Date |
   spr.Date_Of_Event.Date < DateTime.Today.Date |
   scr.Date_Of_Event.Date < DateTime.Today.Date |
   smr.Date_Of_Event.Date < DateTime.Today.Date

在这种情况下,只有一个表将包含日期值,其他表将在其相应的列中包含故障值.

In this scenario only one table will contain date value others will contain dault values in their respected columns.

以上条件不起作用,它是默认值.

The above condition is not working,it takes default value.

我可以在什么条件下获得正确的值?

what condition can i use to get correct value?

推荐答案

|运算符给出的结果与||运算符相同,但不会使结果短路.这意味着即使只能通过评估第一个操作数来确定结果,也总是对两个操作数进行评估.

The | operator gives the same result as the || operator, but it's not short-circuiting the result. That means that both operands are always evaluated even if the result can be determined by evaluating only the first operand.

我认为主要问题是&&运算符的优先级高于|运算符(或||运算符),因此需要括号:

I think that the main problem is that the && operator has higher precedence than the | operator (or the || operator), so you need parentheses:

&& (swr.Date_Of_Event.Date < DateTime.Today.Date |
    spr.Date_Of_Event.Date < DateTime.Today.Date |
    scr.Date_Of_Event.Date < DateTime.Today.Date |
    smr.Date_Of_Event.Date < DateTime.Today.Date)

您可以改用运算符的短路版本,因为读取Date属性没有任何副作用:

You can use the short-circuiting version of the operator instead, as reading the Date properties doesn't have any side effects:

&& (swr.Date_Of_Event.Date < DateTime.Today.Date ||
    spr.Date_Of_Event.Date < DateTime.Today.Date ||
    scr.Date_Of_Event.Date < DateTime.Today.Date ||
    smr.Date_Of_Event.Date < DateTime.Today.Date)

这篇关于| LINQ中的操作员操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 10:09