如果参数为null或空FE,我需要忽略WHERE语句中的部分或全部条件:
我有简单的LINQ查询
var query = from x in context.a
where x.p == param1 && x.i == param2
select x;
Run Code Online (Sandbox Code Playgroud)
x.p == param1如果param1为null或emty,我该如何忽略?
试过这个
var query = from myLog in myContext.ApsValidationLogs
where (myLog.systemtype == comboBoxSystemType.SelectedItem.ToString() || string.IsNullOrEmpty(comboBoxSystemType.SelectedItem.ToString()))
&& (myLog.bankid == comboBoxBankId.SelectedItem.ToString() || string.IsNullOrEmpty(comboBoxBankId.SelectedItem.ToString())))
select myLog;
Run Code Online (Sandbox Code Playgroud)
但得到了
Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)
如果第二个组合框的项目为空.怎么了?
这可能是一个简单的问题,但它是我以前从未见过的答案.有没有办法使用if语句的条件作为其值?在进行大量计算以确定是否满足某个条件的情况下,这将非常有用,如果是,则计算结果.
举个例子:
if ( [intense calculation] > 0, [same intense calculation], 0)
Run Code Online (Sandbox Code Playgroud)
我特别感兴趣的是关于SQL,因为我现在正在使用Access中的报表,因此无法将强烈计算的结果存储在变量中.
前几天我正在寻找一个Ruby代码质量工具,我遇到了pelusa gem,它看起来很有趣.它检查的一件事是给定Ruby文件中使用的else语句的数量.
我的问题是,为什么这些不好?我理解这些if/else语句经常会增加很多复杂性(而且我的目标是降低代码复杂性)但是如何在不使用else?的情况下编写检查两种情况的方法?
总结一下,我有两个问题:
1)除了降低代码复杂性之外还有其他原因可以避免其他语句吗?
2)这是我正在使用的应用程序使用else语句的示例方法.如果没有一个,你会怎么写?我能想到的唯一选择是三元语句,但这里有足够的逻辑,我认为三元语句实际上会更复杂,更难读.
def deliver_email_verification_instructions
if Rails.env.test? || Rails.env.development?
deliver_email_verification_instructions!
else
delay.deliver_email_verification_instructions!
end
end
Run Code Online (Sandbox Code Playgroud)
如果你用三元运算符写这个,那就是:
def deliver_email_verification_instructions
(Rails.env.test? || Rails.env.development?) ? deliver_email_verification_instructions! : delay.deliver_email_verification_instructions!
end
Run Code Online (Sandbox Code Playgroud)
是对的吗?如果是这样,是不是更难以阅读?一份else声明是否有助于打破这种局面?有没有另外一种更好的else方式来写这个我没有想到的东西?
我想我在这里寻找风格的考虑因素.
嗨,我有2个VC++解决方案"A"和"B"(VS2008)都具有相同的代码库(只有几行代码不同).在两者中使用DXVAHD.h.
dxvahd.h是一个标准的Microsoft头文件.如果我们打开这个头文件,我们看到有一个条件,如果" #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)"
我在VC++解决方案"A"中看到,上面的条件#if语句为false,因此整个dxvahd头文件变灰并且甚至没有编译!
而在另一个解决方案"B"中,这个条件#if是真的,因此没有问题及其工作正常.
任何人都可以让我知道如何在解决方案"A"中解决此问题,其中上述#if变得灰色/不编译.PLZ帮帮我.
提前致谢.
c++ conditional conditional-compilation visual-c++ conditional-statements
我正在学习Python,而且我遇到了一些问题.在我正在考虑的课程中看到类似的东西后,想出了这个简短的剧本.我之前用过"或"用"if"成功(这里没有显示太多).出于某种原因,我似乎无法让这个工作:
test = raw_input("It's the flying circus! Cool animals but which is the best?")
x = test.lower()
if x == "monkey" or "monkeys":
print "You're right, they are awesome!!"
elif x != "monkey" or "monkeys":
print "I'm sorry, you're incorrect.", x[0].upper() + x[1:], "is not the right animal."
Run Code Online (Sandbox Code Playgroud)
但是效果很好:
test = raw_input("It's the flying circus! Cool animals but which is the best?")
x = test.lower()
if x == "monkey":
print "You're right, they are awesome!!"
elif x != "monkey":
print …Run Code Online (Sandbox Code Playgroud) 我想对Bash说,等待Process Start/Begin.我正在尝试这种方式,例如:
notepad=`pidof notepad.exe`
until [ $notepad > 0 ]
do
taskset -p 03 $notepad
renice -n 5 -p $notepad
sleep 5
renice -n 0 -p $notepad
done
Run Code Online (Sandbox Code Playgroud)
我有以下问题:
为什么这会生成一个名为"0"的文件(文件为空)我不想创建一个新文件,只是等待PID检查执行.
这是一个循环,但如果2个命令执行correclty,1次我怎么能继续做?
为此更好地使用"直到或"?
等待过程开始或开始的另一个想法???
我有一个meta用3列调用的二维numpy数组..我想要做的是:
我做到了,但解决方案看起来非常人为:
meta[ np.logical_and( np.all( meta[:,0:2] == [0,0],axis=1 ) , meta[:,2] < 20) ]
Run Code Online (Sandbox Code Playgroud)
你能想到更清洁的方式吗?似乎很难同时拥有多个条件;(
谢谢
对不起我第一次复制了错误的表达...纠正了.
我只是想知道Or条件语句如何在VBA/VB6中工作.基本上,如果我们有If A Or B Then,布尔表达式的评估顺序是什么?如果A为真,B还会被评估吗?
我不知道如何将以下条件反应链接到passid表单输入id:
我不知道把它放在哪里,document.querySelector()以便索引的contion-reactions与它相关联.
这是代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Password - form</title>
</head>
<body>
<form>
<label>Password</label>
<input type="password" id="passid" />
<br>
<input type="submit" value="Sign In" />
</form>
<script>
function passType() {
var password = ["p1", "p2", "p3", "p4", "p5"];
if (password.indexOf > -1) {
alert("That's great! Thank you!");
} else {
alert("Wrong, try again");
}
if (password.indexOf > -1) {
alert("It's wrong, you have one more try...");
} else {
alert("Get out now!");
window.close();
}
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个按钮,仅在验证正确完成后将用户重定向到新页面。
有没有办法做这样的事情?如何在类方法内激活路由?
import validator from './validator';
class Example {
constructor(props) {
super(props)
this.saveAndContinue = thos.saveAndContinue.bind(this)
}
saveAndContinue () {
var valid = validator.validate(this.props.form)
if (valid) {
axios.post('/path')
<Redirect to='/other_tab'>
} else {
validator.showErrors()
}
}
render() {
<button onClick={this.saveAndContinue}>Save and Continue</button>
}
}
Run Code Online (Sandbox Code Playgroud) python ×2
bash ×1
c# ×1
c++ ×1
coding-style ×1
conditional ×1
excel ×1
excel-vba ×1
html ×1
if-statement ×1
ignore ×1
javascript ×1
linq ×1
loops ×1
methods ×1
ms-access ×1
numpy ×1
optimization ×1
react-router ×1
reactjs ×1
ruby ×1
sql ×1
vba ×1
visual-c++ ×1
where ×1