Фильтровать по вложенному объекту в списке - PullRequest
1 голос
/ 23 марта 2020

Может кто-нибудь посоветует, пожалуйста, как фильтровать вложенные объекты в LINQ. У меня есть простой объект отношения 1 -> много:

public class WaybillReferences
{
    public int WaybillID { get; set; }
    public string WaybillNo { get; set; }
    public DateTime? WaybillDate { get; set; }
    public string FromBCentre { get; set; }
    public string ToBCentre { get; set; }
    public string FromCustomer { get; set; }
    public string ToCustomer { get; set; }
    public DateTime? PODDate { get; set; }
    public string Signee { get; set; }
    public int? BillingCustomerID { get; set; }
    public List<Order> orderReferences { get; set; }
}

И

public class Order
{
    public string Id { get; set; }
    public int? CustomerId { get; set; }
    public int WaybillId { get; set; }

}

Теперь моя цель - предоставить пользователю фильтр, который будет искать это значение в WaybillReference.WaybillNo И также в List.Id Это то, что я пробовал:

 var data = (from w in _context.Waybill
            join fromCust in _context.Customer on w.PickupCustId equals fromCust.Id
            join toCust in _context.Customer on w.DeliverCustId equals toCust.Id
            join fromBC in _context.Bcentre on fromCust.Pcode equals fromBC.Id
            join toBC in _context.Bcentre on toCust.Pcode equals toBC.Id
            select new WaybillReferences
            {
                WaybillID = w.Id,
                WaybillNo = w.CourierWbno,
                WaybillDate = w.Date,
                PODDate = w.Poddate,
                Signee = w.Signee,
                FromCustomer = fromCust.Name,
                ToCustomer = toCust.Name,
                FromBCentre = fromBC.Name,
                ToBCentre = toBC.Name,
                BillingCustomerID = w.BillCust,
                orderReferences = _context.Order.Where(y => y.WaybillId == w.Id).ToList()
            }).Where(x => BillCustomers.Contains(x.BillingCustomerID)
                   && (fromDate == null || x.WaybillDate >= fromDate)
                   && (toDate == null || x.WaybillDate <= toDate)
                   && (FilterBy.Equals("#") || x.WaybillNo.StartsWith(FilterBy))
                   && (FilterBy.Equals("#") || x.orderReferences.Any(x=> x.Id.StartsWith(FilterBy))) /*Error Here*/
           )
         .OrderBy(z => z.WaybillDate)
         .Take(300);

        return await data.ToListAsync();

Цените ваше время и помощь.

Редактировать: Ошибка, которую я получаю

InvalidOperationException: выражение LINQ 'DbSet .Join (внешний: DbSet, внутренний: w => w.PickupCustId, outerKeySelector: c => (Nullable) c .Id, innerKeySelector: (w, c ) => новый TransparentIdentifier (Outer = w, Inner = c)) .Join (внешний: DbSet, внутренний: ti => ti.Outer.DeliverCustId, outerKeySelector: c0 => (Nullable) c0.Id, innerKeySelector: ( ti, c0) => new TransparentIdentifier, Customer> (Outer = ti, Inner = c0)) .Join (внешний: DbSet, внутренний: ti0 => ti0.Outer.Inner.Pcode, outerKeySelector: b => b.Id, innerKeySelector: (ti0, b) => новый TransparentIdentifier, Customer>, Bcentre> (Outer = ti0, Inner = b)) .Join (внешний: DbSet, в ner: ti1 => ti1.Outer.Inner.Pcode, outerKeySelector: b0 => b0.Id, innerKeySelector: (ti1, b0) => new TransparentIdentifier, Customer>, Bcentre>, Bcentre> (Outer = ti1, Inner = b0 )). Где (ti2 => __BillCustomers_0.Contains (ti2.Outer.Outer.Outer.Outer.BillCust) && False || ti2.Outer.Outer.Outer.Outer.Date> = __fromDate_1 && True && False || __FilterBy_2 == "" || ti2.Outer.Outer.Outer.Outer.CourierWbno! = null && __FilterBy_2! = null && ti2.Outer.Outer.Outer.Outer.CourierWbno.StartsWith (__ FilterBy_2) && DbSet .Where (o => o.Wayb .Outer.Outer.Outer.Outer.Id) .ToList () .Any (c => False || __FilterBy_2 == "" || c .Id! = Null && __FilterBy_2! = Null && c .Id.StartsWith (__ FilterBy_2))) 'не может быть переведен. Либо переписать запрос в форме, которую можно перевести, либо явно переключиться на оценку клиента, вставив вызов либо AsEnumerable (), AsAsyncEnumerable (), ToList (), либо ToListAsyn c (). См. https://go.microsoft.com/fwlink/?linkid=2101038 для получения дополнительной информации.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...