我有一个.Any()Linq方法:
<ion-content>
即返回错误:
db.MyTable.Any(x => x.Year == MyObj.Year && x.Quarter == MyObj.Quarter && x.Week == MyObj.Week)
然而,MSDN文档指出.Any方法返回&#34;如果源序列包含任何元素,则返回true;否则,假。&#34;
为什么这个方法抛出异常而不是返回False?
答案 0 :(得分:0)
使用这个小代码很难看出原因是什么。它肯定不在代码的这一部分。
尝试进行一些调试,使用调试器检查所有值,或者在执行函数之前编写一些Debugging语句:
// Check myObj
MyObjectType myObj = ... // instead of var, so you are certain you have the right type
Debug.Assert(myObj != null);
// only if Year, Quarter, Week are classes:
Debug.Assert(myObj.Year != null);
Debug.Assert(myObj.Quarter != null);
Debug.Assert(myObj.Week != null);
// check your db:
Debug.Assert(db != null);
Debug.Assert(db.MyTable != null);
int count1 = db.MyTable.Count();
int count2 = db.MyTable
.Where(x => x.Year == MyObj.Year
&& x.Quarter == MyObj.Quarter
&& x.Week == MyObj.Week)
.Count();
bool hasAny = db.MyTable
.Where(x => x.Year == MyObj.Year
&& x.Quarter == MyObj.Quarter
&& x.Week == MyObj.Week)
.Any();
// if all this works, your statement should also work
hasAny = db.MyTable
.Any(x => x.Year == MyObj.Year
&& x.Quarter == MyObj.Quarter
&& x.Week == MyObj.Week);
答案 1 :(得分:-1)
很难看到,您的示例可能必须检查“MyTable”中是否有Null值,MyTable.Year,MyTable.Quarter和MyTable.Week的数据类型与MyObj中的相同内容匹配... < / p>