# 最大或默认？

``Dim x = (From y In context.MyTable _ Where y.MyField = value _ Select y.MyCounter).Max` `

` `Dim x = (From y In context.MyTable _ Where y.MyField = value _ Select y.MyCounter _ Order By MyCounter Descending).FirstOrDefault` `

` `Dim x = (From y In context.MyTable _ Where y.MyField = value _ Select CType(y.MyCounter, Integer?)).Max` `

` `var x = (from y in context.MyTable where y.MyField == value select (int?)y.MyCounter).Max();` `

` `int max = list.Max(i => (int?)i.MyCounter) ?? 0;` `

` `Dim x = (From y In context.MyTable _ Where y.MyField = value _ Select y.MyCounter).DefaultIfEmpty.Max` `

{1，2，3，-1，-2，-3}的最大值显然是3. {2}的最大值明显是2.但空集{}的最大值是多less？ 显然这是一个毫无意义的问题。 空集的最大值没有被定义。 试图得到答案是一个math错误。 任何集合的最大值本身都是该集合中的一个元素。 空集没有元素，所以声称某个特定数是该集中的最大值，而不是在该集中是一个math上的矛盾。

` `double x = context.MyTable .Where(y => y.MyField == value) .Select(y => y.MyCounter) .Concat(new double[]{Double.MinValue}) .Max();` `
` `int max = list.Any() ? list.Max(i => i.MyCounter) : 0;` `

` `int max = (from e in context.Table where e.Year == year select e.RecordNumber).DefaultIfEmpty(0).Max(); DateTime maxDate = (from e in context.Table where e.Year == year select e.StartDate ?? DateTime.MinValue).DefaultIfEmpty(DateTime.MinValue).Max();` `

` `Try Dim x = (From y In context.MyTable _ Where y.MyField = value _ Select y.MyCounter).Max ... continue working with x ... Catch ex As SqlException ... do error processing ... End Try` `

` `from y in context.MyTable group y.MyCounter by y.MyField into GrpByMyField where GrpByMyField.Key == value select GrpByMyField.Max()` `

` `SELECT [t1].[MaxValue] FROM ( SELECT MAX([t0].[MyCounter) AS [MaxValue], [t0].[MyField] FROM [MyTable] AS [t0] GROUP BY [t0].[MyField] ) AS [t1] WHERE [t1].[MyField] = @p0` `

` `(From y In context.MyTable _ Where y.MyField = value _ Select y.MyCounter)` `

` `var max = new[]{0} .Concat((From y In context.MyTable _ Where y.MyField = value _ Select y.MyCounter)) .Max();` `

` `Dim x = context.MyTable.Max(Function(DataItem) DataItem.MyField = Value)` `

` `var max = new[]{0} .Concat((From y In context.MyTable _ Where y.MyField = value _ Select y.MyCounter)) .Max();` `

System.NotSupportedException：LINQ to Entities不支持LINQexpression式节点types“NewArrayInit”。

` `(From y In context.MyTable _ Where y.MyField = value _ Select y.MyCounter)) .OrderByDescending(x=>x).FirstOrDefault());` `

` `decimal Max = (decimal?)(context.MyTable.Select(e => e.MyCounter).Max()) ?? 0;` `

（我嘲笑select（）在我的testing）

` `var requiredDataQuery = _dataRepo.Select(x => new { x.NullableDate1, .NullableDate2 }); var requiredData.ToList(); var maxDate1 = dates.Max(x => x.NullableDate1); var maxDate2 = dates.Max(x => x.NullableDate2);` `

` ` public static TResult MaxOrDefault<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult?>> selector, TResult defaultValue = default (TResult)) where TResult : struct { return source.Max(selector) ?? defaultValue; }` `

` ` sequence.DefaultOrMax(s => (int?)s.Id);` `

` `static class Extensions { public static TResult MaxOrDefault<T, TResult>(this IQueryable<T> source, Expression<Func<T, TResult>> selector) where TResult : struct { UnaryExpression castedBody = Expression.Convert(selector.Body, typeof(TResult?)); Expression<Func<T, TResult?>> lambda = Expression.Lambda<Func<T,TResult?>>(castedBody, selector.Parameters); return source.Max(lambda) ?? default(TResult); } }` `

` `int maxId = dbContextInstance.Employees.MaxOrDefault(employee => employee.Id); // maxId is equal to 0 if there is no records in Employees table` `