C# 如何编写OrderBy lambda表达式的方法
我有密码:C# 如何编写OrderBy lambda表达式的方法,c#,lambda,C#,Lambda,我有密码: var trips = _db.Trips .OrderBy(u => u.TripStops.Where(p=>p.StopTypeId == destinationTypeId).OrderByDescending(c=>c.StopNo).Select(p=>p.Appt).FirstOrDefault()) 这一部分(对最后一个TripStop按appt排序,type=destinati
var trips = _db.Trips
.OrderBy(u => u.TripStops.Where(p=>p.StopTypeId == destinationTypeId).OrderByDescending(c=>c.StopNo).Select(p=>p.Appt).FirstOrDefault())
这一部分(对最后一个TripStop按appt排序,type=destinationTypeId)应该在代码中的许多地方使用
我想写一个方法,比如:
private xxx LastTripStopAppt(...)
{
}
然后像这样使用它:
var trips = _db.Trips
.OrderBy(LastTripStopAppt(u))
但是对于如何正确实现这个方法有点困惑
顺便说一句,我已经尽力做到了
private DateTime? ReturnLastDeliveryAppointment(Trip u, int destinationTypeId)
{
return u.TripStops.Where(p => p.StopTypeId == destinationTypeId).OrderByDescending(c => c.StopNo).Select(p => p.Appt).FirstOrDefault();
}
然后
.OrderBy(u => ReturnLastDeliveryAppointment(u, destinationTypeId))
但我有一个错误:
LINQ to实体无法识别该方法
'System.Nullable'1[System.DateTime]
ReturnLastDeliveryAppointment(Infrastructure.Asset.Trips.Trip,Int32)
方法,而此方法无法转换为存储表达式
签名可能类似于:
private Expression<Func<Trip, apptType>> LastTripStopAppt(...)
挑选*
从(值(1)、(4)、(3))t(v)
按T排序
Java 8:
流s=流(11,41,3)
s、 排序()
.forEach(System.out::println)
输出:
311
41你尝试了什么?@Tomer添加了我的代码。。。你把这个贴错问题了吗?这似乎与这个问题不太相关。。。
private static Expression<Func<Trip, string>> LastTripStopAppt(int destinationTypeId)
{
return u => u.TripStops.Where(p=>p.StopTypeId == destinationTypeId).OrderByDescending(c=>c.StopNo).Select(p=>p.Appt).FirstOrDefault();
}