Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

c# - LINQ to Entities does not recognize the method in query

I have the following code that gives me an error saying that LINQ to Entities does not recognize the method GetShippingHour:

private IEnumerable<OrderDTO> GetAllFixedOrders(DateTime date)
    {
        var clientSet = _context.Set<Client>();
        var orderSubstitutes = _context.Set<OrderSubstitution>()
                                .Include(o=>o.Order.Client)
                                .ForCurrentDay(date)
                                .Select(os => new { os.OrderId, os.FixedOrderId });

        var orderList = _context.Set<FixedOrder>()
            .Include(fo => fo.Client)
           .ForCurrentDay(date)
           .Where(fo => !fo.Client.IsInactive)
           .ConsideringOrderSubstitution(orderSubstitutes.Select(os => os.FixedOrderId).ToList())
           .Join(clientSet
               , o => o.ClientId
               , c => c.Id
               , (o, c) =>
                   new OrderDTO
                   {
                       OrderId = orderSubstitutes.Where(os => os.FixedOrderId == o.Id).Select(os => os.OrderId).FirstOrDefault() ?? 0,
                       ClientId = c.Id,
                       ClientName = c.Name,
                       HasFixedOrders = true,
                       FixedOrderId   = o.Id,
                       ShippingHour = GetShippingHour(c,date),
                       HasSpecialProducts = false
                   }
           ).ToList();

        return orderList;
    }

    private string GetShippingHour(Client c, DateTime date)
    {
        var dayOfWeek = date.DayOfWeek.ToString();
        string result;
        switch (dayOfWeek)
        {
            case "Sunday":
                result = c.ShippingHourSunday ?? c.ShippingHour;
                break;
            case "Monday":
                result = c.ShippingHourMonday ?? c.ShippingHour;
                break;
            case "Tuesday":
                result = c.ShippingHourTuesday ?? c.ShippingHour;
                break;
            case "Wednesday":
                result = c.ShippingHourWednesday ?? c.ShippingHour;
                break;
            case "Thursday":
                result = c.ShippingHourThursday ?? c.ShippingHour;
                break;
            case "Friday":
                result = c.ShippingHourFriday ?? c.ShippingHour;
                break;
            case "Saturday":
                result = c.ShippingHourSaturday ?? c.ShippingHour;
                break;
            default:
                result = c.ShippingHour;
                break;
        }
        return result;
    }

The problem is that, depending on the day of the week, I need to get the value for ShippingHour from a different field of the Clients table. The problem is that LINQ to Entities is trying to translate my method. I don't know how to rewrite this query to do the required results.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
    var orderList = _context.Set<FixedOrder>()
        .Include(fo => fo.Client)
       .ForCurrentDay(date)
       .Where(fo => !fo.Client.IsInactive)
       .ConsideringOrderSubstitution(orderSubstitutes.Select(os => os.FixedOrderId).ToList())
       .Join(clientSet
           , o => o.ClientId
           , c => c.Id
           , (o, c) =>
               new 
               {
                   OrderId = orderSubstitutes.Where(os => os.FixedOrderId == o.Id).Select(os => os.OrderId).FirstOrDefault() ?? 0,
                   Client = c,
                   HasFixedOrders = true,
                   FixedOrderId   = o.Id,
                   HasSpecialProducts = false
               }
       ).ToList()
       .Select(t=>new OrderDTO {
                   OrderId = t.OrderId,
                   ClientId = t.Client.Id,
                   ClientName = t.Client.Name,
                   HasFixedOrders = t.HasFixedOrders,
                   FixedOrderId   = t.FixedOrderId,
                   ShippingHour = GetShippingHour(t.Client,date),
                   HasSpecialProducts = t.HasSpecialProducts
       }).ToList();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...