c# – .Single或Default with condition或Where子句
发布时间:2020-12-15 05:39:37 所属栏目:百科 来源:网络整理
导读:我有以下代码 return this.Storage.Customer.OfTypePreferred() .Include(b = b.Order) .Where(cust = cust.Id == customerId cust.CustomerType== (int)cusType) .SingleOrDefault(); 它可以改写如下消除where. return this.Storage.Customer.OfTypePreferr
我有以下代码
return this.Storage.Customer.OfType<Preferred>() .Include(b => b.Order) .Where(cust => cust.Id == customerId && cust.CustomerType== (int)cusType) .SingleOrDefault(); 它可以改写如下消除where. return this.Storage.Customer.OfType<Preferred>() .Include(b => b.Order) .SingleOrDefault(cust => cust.Id == customerId && cust.CustomerType == (int)cusType); 哪一个更好实践,为什么? 解决方法
首先,您需要了解其中的差异
this.Storage.Customer.OfType<Preferred>() .Include(b => b.Order) .Where(cust => cust.Id == customerId && cust.CustomerType== (int)cusType) 这只会创建一个查询但不会执行,直到你调用ToList方法. SingleOrDefault方法实际上将执行查询.因此,如果您想在查询执行之前检查或执行某些操作,则应使用where,然后调用SingleOrDefault. 总的来说,根据我个人的意见,使用哪里是好的做法 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |