这是一个 LinqToSql 的小陷阱:
// Returns the number of counties in a state,
// or all counties in the USA if the state is null
public static int CountCounties(State s) {
var q =
from cy in County.GetTable() // my method to get the ITable
where (s == null || s.Code == cy.StateCode) // shortcut OR operator, right...?
select cy;
return q.Count();
}
猜猜看 - 如果你传递一个 nullState
对象此方法,你会得到一个空引用异常! LinqToSql 似乎没有使用||
快捷操作符作为快捷方式!
对此提出最佳解释和解决方法的人将得到答复。
如果是 linq to sql,请记住 Linq 只是将您的查询解析为 SQL。
因此,它将两个 where 子句发送到数据库,因此出现异常。我并不觉得这真的令人惊讶,尽管它可以说是错误的。
您只需要进行独立检查即可。
if (!string.isNullOrEmpty(state.statecode)
q = q.where( s => s.code == state.statecode
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)