Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在查询linq中避免OrderByDescending_C#_Linq - Fatal编程技术网

C# 如何在查询linq中避免OrderByDescending

C# 如何在查询linq中避免OrderByDescending,c#,linq,C#,Linq,我有linq查询: var ed = db.table .GroupBy(x => x.Sn) .Select(g => g.OrderByDescending(x => x.Date).FirstOrDefault()); 我需要重写此查询以进行服务器端评估 我的桌子: Sn Value Data 150 180.3 01/06/2020 150 195.0 01/05/2020 149 13.3 01/06

我有linq查询:

var ed = db.table
.GroupBy(x => x.Sn)
.Select(g => g.OrderByDescending(x => x.Date).FirstOrDefault());
我需要重写此查询以进行服务器端评估

我的桌子:

Sn    Value      Data     
150   180.3    01/06/2020  
150   195.0    01/05/2020  
149   13.3     01/06/2020  
345   27.5     27/06/2013
....  
可能只是:

.Select(g => g.Max(x => x.Date))

解析器可能处理得更好

您可以尝试使用
Aggregate

var ed = db.table
           .GroupBy(x => x.Sn)
           .Select(x => x.Aggregate((max, cur) => max.Date > cur.Date ? max : cur));

这可能有助于您了解更多信息。

这取决于
dt.Table
是否可用

通常IQueryable由不同的进程执行,通常是数据库管理系统。在这种情况下,必须使用OrderBy,后跟FirstOrDefault

幸运的是,适当的数据库管理系统对排序进行了极大的优化。如果您对排序的效率不满意,并且您不经常更改表,请考虑在DbWordNo.OnMe建模创建中添加一个额外的索引:

modelBuilder.Entity<Customer>()
    .HasIndex(customer => customer.Name)

如果你经常使用这个方法,考虑创建一个扩展方法。创建扩展方法非常简单。看


这里的问题是什么?您在这个查询中面临什么问题?您遇到了什么错误?如果执行此查询,会出现以下异常:OrderByDescending(x=>x.Date)无法翻译。以可以翻译的形式重写查询,或者通过插入对AsEnumerable()、AsAsAsAsyncEnumerable()、ToList()或ToListSync()的调用显式切换到客户端计算。有关详细信息,请参阅。您需要按日期按降序排列数据?我需要为每个ID选择具有最大日期的所有对象。我已经在此处提出了类似的问题,但该答案仅适用于客户端。不幸的是,我的表包含大量数据,我无法负担客户端计算。使用两个查询或写入并执行原始查询它会为每个对象选择最大日期还是表中的最大日期?@disshell as write:组中的最大值,这与“降序、第一或默认顺序”相同不幸的是,此查询返回IQueryable,但我需要从表中获取完整对象。
modelBuilder.Entity<Customer>()
    .HasIndex(customer => customer.Name)
var result = db.table.GroupBy(x => x.Sn)
   .Aggregate( (maxItem, nextitem) =>(nextItem.Date > maxItem.Date) ?? nextItem : maxItem)
public static T MaxOrDefault<T, TProperty> MaxPropertyOrDefault(
    this IEnumerable<T> source,
    Func<TSource, TProperty> propertySelector)
{
     return MaxPropertyOrDefault(source, propertySelector, null)
}
public static T MaxOrDefault<T, TProperty> MaxPropertyOrDefault(
    this IEnumerable<T> source,
    Func<TSource, TProperty> propertySelector,
    IComparer<TProperty) comparer)
{
    // TODO: what to do if source == null?
    // TODO: what to do if propertySelector == null?
    if (comparer == null) comparer = Comparer<TProperty>.Default();
    
    var enumerator = source.GetEnumerator();
    if (!enumerator.MoveNext)
    {
        // empty source, return default:
        return default(T);
    }
    else
    {
        TProperty maxPropertyValue = propertySelector(enumerator.Current);
        T maxValue = enumerator.Current();

        while (enumerator.MoveNext())
        {
             TProperty currentPropertyValue = propertySelector(enumerator.Current);
             if (comparer.Compare(currentPropetyValue, maxPropertyValue) > 0)
             {
                 maxPropertyValue = currentPropertyValue;
                 maxValue = enumerator.Current;
             } 
        }
        return maxValue;
    }
}
var ed = db.table.GroupBy(x => x.Sn)
           .Select(group => group.MaxOrDefault(groupElement => groupElement.Date);