C#实体框架LINQ向实体在SELECT表达式下添加参数WHERE表达式

时间:2018-10-17 01:10:37

标签: c# entity-framework linq-to-sql entity-framework-6 linq-to-entities

我试图通过将Id参数传递给WHERE表达式来选择某种类型的导航属性,但是它失败并出现以下错误;

“从范围”引用的类型为'X.Models.ParkingLot'的变量'parkingLot',但未定义”

通过搜索S / O,我了解的(如果我没有记错的话)LINQ to Entities无法理解函数合并表达式。因此,我想问的是,有没有办法实现此功能。

我目前正在尝试做的事

BaseService层包含SelectAsync Function Expression>这样;

    public virtual async Task<List<TOut>> SelectAsync<TIn, TOut>(Expression<Func<TIn, TOut>> selector) 
        where TIn : class, IBaseModel
    {
        List<TOut> retVal;
        using (var dc = new TContext() { Configuration = { ProxyCreationEnabled = false, AutoDetectChangesEnabled = false, LazyLoadingEnabled = false } })
        {
            retVal = await dc.Set<TIn>().AsNoTracking().Select(selector).ToListAsync();
        }
        return retVal;
    }

从服务部门,我正在致电相关实体;

    var parkingLots = await SelectAsync(ParkingLotResponse.ResponseSelectExpr);

ParkingLotResponse.ResponseSelectExpr随后;

    public class ParkingLotResponse
    {
        public int Id { get; set; }

        .
        .
        .

        public IEnumerable<ParkingLotServiceResponse> Services { get; set; }

        .
        .
        .

        public static Expression<Func<ParkingLot, ParkingLotResponse>> ResponseSelectExpr =
            parkingLot => new ParkingLotResponse()
            {
                Id = parkingLot.Id,
                .
                .
                .
                // If we use non-function bodied (which is right below this line), everything runs ok.
                // Services = parkingLot.Services.AsQueryable().Select(ParkingLotServiceResponse.ResponseSelectExpr),
                Services = parkingLot.Services.AsQueryable().Select(ParkingLotServiceResponse.ResponseSelectExprParkingLot(parkingLot.Id)), // This one is causing problem
                .
                .
                .
            };

    }

ParkingLotServiceResponse在哪里;

public class ParkingLotServiceResponse
{
    public int Id { get; set; }

    public IEnumerable<ParkingLotServiceFareResponse> ServiceFares { get; set; }

        // This one is ok but can not select related subitems, thus returns all subitems
    public static Expression<Func<ParkingService, ParkingLotServiceResponse>> ResponseSelectExpr =>
        parkingLotService => new ParkingLotServiceResponse()
        {
            Id = parkingLotService.Id,
            ServiceFares = parkingLotService.ServiceFares.AsQueryable()
            .Select(ParkingLotServiceFareResponse.ResponseSelectExpr).AsEnumerable()
        };

        // This one is causing problem
    public static Expression<Func<ParkingService, ParkingLotServiceResponse>> ResponseSelectExprParkingLot(Guid parkingLotId) =>
        parkingLotService => new ParkingLotServiceResponse()
        {
            Id = parkingLotService.Id,
            ServiceFares = parkingLotService.ServiceFares.AsQueryable()
                .Where(ParkingLotServiceFareResponse.WhereBelongsToParkingLot(parkingLotId))
                //.Where(plsf => plsf.ParkingLotId == parkingLotId) // -> this is not working either
                .Select(ParkingLotServiceFareResponse.ResponseSelectExpr).AsEnumerable()
        };

}

ParkingLotServiceFareResponse在哪里;

public class ParkingLotServiceFareResponse
{
    public string Description { get; set; }

    public decimal Price { get; set; }

    public static Expression<Func<ParkingLotServiceFare, ParkingLotServiceFareResponse>> ResponseSelectExpr =>
        parkingLotServiceFare => new ParkingLotServiceFareResponse()
        {
            Description = parkingLotServiceFare.Description,
            Price = parkingLotServiceFare.Price
        };

    public static Expression<Func<ParkingLotServiceFare, bool>> WhereBelongsToParkingLot(Guid parkingLotId) =>
        parkingLotServiceFare => parkingLotServiceFare.ParkingLotId.Equals(parkingLotId);

}

任何帮助将不胜感激。

谢谢

0 个答案:

没有答案