[英]How to write a method for OrderBy lambda expression
我有代码:
var trips = _db.Trips
.OrderBy(u => u.TripStops.Where(p=>p.StopTypeId == destinationTypeId).OrderByDescending(c=>c.StopNo).Select(p=>p.Appt).FirstOrDefault())
并且此部分(按appt排序,最后一个TripStop类型= destinationTypeId)应在代码中的许多地方使用。
我想写一个像这样的方法:
private xxx LastTripStopAppt(...)
{
}
然后像这样使用它:
var trips = _db.Trips
.OrderBy(LastTripStopAppt(u))
但是有点困惑如何正确实现此方法。
PS。 我试图这样做
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 Entities无法识别方法'System.Nullable`1 [System.DateTime] ReturnLastDeliveryAppointment(Infrastructure.Asset.Trips.Trip,Int32)'方法,并且该方法无法转换为商店表达式。
签名可能类似于:
private Expression<Func<Trip, apptType>> LastTripStopAppt(...)
其中appType
是p.Appt
的类型
您需要将destinationTypeId
作为参数传递给此方法。
因此,如果appType
是一个string
:
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();
}
选择* FROM(VALUES(1),(4),(3))t(v)OR BY BY T;
Java 8:
流s = Stream.of(11,41,3);
s.sorted().forEach(System.out :: println);
输出:
3
11
41
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.