1
我在这里有点困惑。如果我的查询没有返回任何为什么isnt coupon null?我应该如何检查null?实体框架不返回null
Coupon coupon;
using (var db = new KupongEntities())
{
coupon = (from p in db.Coupon
where p.CouponID == 123
select p).SingleOrDefault();
}
if (coupon != null)
{
//test
}
您正在使用SingleOrDefault,因此它将返回Coupon的一个实例,或者如果在您的集合中找不到任何内容,则返回null。 – ken2k 2012-02-13 09:57:51
它实际上会返回默认(优惠券),这可能是一个新的优惠券,你可以重新定义你的对象的默认值 – 2012-02-13 10:01:52