我有这个疑问:
INT maxShoeSize = Workers.Where(X => x.CompanyId == 8)的.max(X => x.ShoeSize);
将在 maxShoeSize
如果公司8没有工人呢?
更新: 我怎样才能修改查询,以获得0,而不是一个例外?
解决方案 INT maxShoeSize = Workers.Where(X => x.CompanyId == 8)
。选择(X => x.ShoeSize)
.DefaultIfEmpty(0)
的.max();
在零 DefaultIfEmpty
是没有必要的。
I have this query:
int maxShoeSize=Workers.Where(x=>x.CompanyId==8).Max(x=>x.ShoeSize);
What will be in maxShoeSize
if company 8 has no workers at all?
UPDATE: How can I change the query in order to get 0 and not an exception?
解决方案int maxShoeSize = Workers.Where(x => x.CompanyId == 8)
.Select(x => x.ShoeSize)
.DefaultIfEmpty(0)
.Max();
The zero in DefaultIfEmpty
is not necessary.