最大的返回值是否为空查询为空、返回值、最大

2023-09-03 04:41:31 作者:夙兴夜寐.

我有这个疑问:

  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 是没有必要的。

关于mysql可重复读的一些问题

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.