Linq dynamically add where

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
361
Programming Experience
10+
Hello there,

I have this search page.
C#:
<div class="m-4">
    <div class="row g-6">
        <div class="col-3">
            <label class="form-label">Vendor</label>
            <RadzenDropDownDataGrid TValue="int" AllowFiltering="true" AllowClear="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" FilterOperator="StringFilterOperator.Contains"
                                    Data=@_vendors Count="5" TextProperty="Name" ValueProperty="Id" Change=@(args => OnChange(args))
                                    Class="w-100"/>

        </div>
        <div class="col-3">
            <label class="form-label">Order Date Start</label>
            <RadzenDatePicker TValue="DateTime" DateFormat="d" Class="w-100" Change=@(args => OnStartDateChange(args)) />
        </div>
        <div class="col-3">
            <label class="form-label">Order Date End</label>
            <RadzenDatePicker TValue="DateTime" DateFormat="d" Class="w-100" Change=@(args => OnEndDateChange(args)) />
        </div>
        <div class="col-3">
            <label class="form-label">Order Status</label>
            <RadzenDropDown AllowClear="true" TValue="string" Class="w-100" Data=@orderStatusList Name="Status" Change=@(args => OnStatusChange(args))/>
        </div>
    </div>
    <div class="mt-5">
        <div class="d-flex flex-grow-1 justify-content-center align-items-center">
            <button type="button" class="btn btn-primary w-25" @onclick="ExportExcel">
                Orders Excel Export
            </button>
        </div>
    </div>
</div>

Based on selections of those inputs on the page, I am trying to query database.
C#:
public async Task<IEnumerable<Order>> GetOrdersForExport(int vendorId,string status, DateTime? startDateTime, DateTime? endDateTime)
        {
            IQueryable<Order?> result = _db.Orders.Include(d => d.OrderDetails).ThenInclude(v => v.Vendor);

            if (vendorId != null)
            {
                result = result.Where(d => d.OrderDetails.All(s => s.VendorId == vendorId));
            }

            if (status != null)
            {
                result = result.Where(dy => dy.Status == status);
            }
            
            return await result.ToListAsync();
        }

The problem is I can't get the right order detail from the query.

C#:
SET IDENTITY_INSERT [dbo].[Orders] ON
INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [CustomerName], [Status], [DoneBy]) VALUES (1, N'2022-06-07 16:46:21', N'customer1', N'Completed', N'test1111')
INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [CustomerName], [Status], [DoneBy]) VALUES (2, N'2022-06-08 00:00:00', N'Vestel', N'Continues', N'Cenk')
INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [CustomerName], [Status], [DoneBy]) VALUES (3, N'2022-07-09 00:00:00', N'Arçelik', N'Cancelled', N'Cavit')
INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [CustomerName], [Status], [DoneBy]) VALUES (4, N'2022-08-08 23:31:59', N'Fener', N'Continues', N'Test')
INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [CustomerName], [Status], [DoneBy]) VALUES (5, N'2022-08-08 23:37:54', N'Ümraniye', N'Continues', N'Ümraniye')
INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [CustomerName], [Status], [DoneBy]) VALUES (6, N'2022-08-04 23:43:28', N'Bruma', N'Completed', N'Gustova')
INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [CustomerName], [Status], [DoneBy]) VALUES (7, N'2022-09-09 13:16:36', N'Custom', N'Completed', N'Test')
INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [CustomerName], [Status], [DoneBy]) VALUES (8, N'2022-08-09 13:21:14', N'Tsubasa', N'Continues', N'Employee')
INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [CustomerName], [Status], [DoneBy]) VALUES (9, N'2022-10-27 13:37:36', N'New Customer', N'Completed', N'Test')
INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [CustomerName], [Status], [DoneBy]) VALUES (10, N'2022-08-18 13:36:28', N'Wonderland', N'Continues', N'test')
INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [CustomerName], [Status], [DoneBy]) VALUES (11, N'2022-08-20 14:12:44', N'Schumacher', N'Continues', N'Ferrari')
SET IDENTITY_INSERT [dbo].[Orders] OFF

SET IDENTITY_INSERT [dbo].[OrdersDetail] ON
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (4, N'1', N'r', 45, 27, 0, 56, 4, N'shipment', N'Completed', N'001', N'desc', 1, 2, N'Dolar', 4.5, 5.3)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (5, N'12345-ABCN', N'Ipad', 5, 10, 1000, 1000, 1500, N'S1', N'Getting ready', N'T11111', N'description1', 2, 1, N'TL', 0, 0)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (6, N'0000-DDDD', N'Modem', 6, 15, 200, 150, 225, N'S2', N'Getting ready', N'T11112', N'description2', 2, 2, N'TL', 0, 0)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (7, N'98937-OPSKJ', N'ZTE', 7, 10, 5000, 4500, 5500, N'S3', N'Getting ready', N'T11113', N'description3', 2, 1, N'TL', 0, 0)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (8, N'11111-YUSH', N'Laptop', 8, 6, 50, 50, 75, N'S4', N'Cancelled', N'T11114', N'description1', 3, 2, N'Dolar', 0, 0)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (9, N'0090', N'Knife', 154, 30, 0, 700, 343, N'test', N'Completed', N'ueı9q9u', N'test', 1, 2, N'Euro', 5, 7.5)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (10, N'4', N'mouse', 10, 10, 0, 10, 10, N's', N'Cancelled', N't', N'd', 3, 1, N'Euro', 0, 0)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (11, N'rrrrrrrr', N'tank palet', 8878, 40, 0, 288, 39, N'trk*0292', N'Getting ready', N'79ıwowu998', N'desc', 4, 1, N'Dolar', 0, 0)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (12, N'11112', N'ccccc2', 4, 4, 0, 4, 4, N'3332', N'In warehouse', N'3332', N'ttt', 9, 3, N'Euro', 0, 0)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (13, N'444', N'rrrr', 2, 2, 0, 2, 2, N'ttt', N'Getting ready', N'5444', N'g', 9, 1, N'Dolar', 0, 0)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (14, N'rrr', N'tttt', 5, 15, 0, 8, 9, N'uuu', N'At customs', N'6454', N'gtdd', 10, 1, N'Dolar', 0, 0)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (15, N'2345', N'Cenk', 10, 13, 0, 0, 0, N'0000', N'Getting ready', N'0000', N'desc', 1, 3, N'TL', 3, 3.8)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (16, N'3444', N'car', 1, 22, 0, 0, 0, N'ttt', N'Shipped', N'4444', N'd', 11, 2, N'Dolar', 150000, 175000)
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (17, N'8', N'd', 2, 4, 0, 0, 0, N'd', N'At customs', N'r', N'r', 11, 3, N'TL', 2, 10)
SET IDENTITY_INSERT [dbo].[OrdersDetail] OFF

Supposed to retrieve only orderdetail id = 13 when search for vendorId = 1 and order status = "Completed" But LINQ query brings more. Where is the problem?

Thanks in advance.
 
I think your expectations don't match your data: OrderDetail ID = 13, seems to be in "Getting ready" status.
C#:
INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice])
    VALUES (13, N'444', N'rrrr', 2, 2, 0, 2, 2, N'ttt', N'Getting ready', N'5444', N'g', 9, 1, N'Dolar', 0, 0)
 
Here is brand new test data for you, hope you can solve the puzzle :)

SQL:
     SET IDENTITY_INSERT [dbo].[Orders] ON
     INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [Status], [DoneBy], [CustomerId]) VALUES (12, N'2022-08-25 12:09:33', N'Continues', N'Cenk', 5)
     INSERT INTO [dbo].[Orders] ([Id], [OrderDateTime], [Status], [DoneBy], [CustomerId]) VALUES (13, N'2022-08-28 17:44:46', N'Completed', N'Sedat', 6)
     SET IDENTITY_INSERT [dbo].[Orders] OFF
        
     SET IDENTITY_INSERT [dbo].[OrdersDetail] ON
     INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (18, N'123456', N'Modem', 10, 12, 0, 0, 0, N'ttt', N'Getting ready', N'1224242', N'ffgff', 12, 1, N'TL', 3.5, 4.2)
     INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (19, N'444', N'Fridge', 50, 18, 0, 0, 0, N'y', N'At customs', N'65yu77', N'yyyuuuu', 12, 2, N'Dolar', 1.4, 2.2)
     INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (20, N'3333', N'home', 12, 13, 0, 0, 0, N'ship', N'Completed', N'63737jkkkk', N'dikkat', 13, 1, N'Euro', 5, 6.2)
     INSERT INTO [dbo].[OrdersDetail] ([Id], [ProductCode], [ProductName], [Quantity], [CostRatio], [UnitCost], [TotalBuyPrice], [TotalSellPrice], [ShippingNumber], [Status], [TrackingNumber], [Description], [OrderId], [VendorId], [Currency], [BuyUnitPrice], [SellUnitPrice]) VALUES (21, N'uyuyeh778', N'test', 40, 12, 0, 0, 0, N'fddfd', N'Completed', N'434343', N'fgdgdfd', 13, 1, N'TL', 8, 11)
     SET IDENTITY_INSERT [dbo].[OrdersDetail] OFF
     SET IDENTITY_INSERT [dbo].[Vendors] ON
     INSERT INTO [dbo].[Vendors] ([Id], [Name], [Address], [Email], [PhoneNumber], [MainResponsibleName], [AssistantResponsibleName]) VALUES (1, N'Test Vendor1', N'Test Address1', N'testemail@mail.com', N'123456789', N'responsible1', N'responsible2')
     INSERT INTO [dbo].[Vendors] ([Id], [Name], [Address], [Email], [PhoneNumber], [MainResponsibleName], [AssistantResponsibleName]) VALUES (2, N'Test vendor2', N'Address2', N'vendor@vendor.com', N'000000000011122', N'Main Resp1', N'Assitant Resp1')
     INSERT INTO [dbo].[Vendors] ([Id], [Name], [Address], [Email], [PhoneNumber], [MainResponsibleName], [AssistantResponsibleName]) VALUES (3, N'Add Names', N'duadadajdajda', N'mail@mail.comd', N'8329392842823', N'kdakdla daşd', N'dşakdşadad')
     SET IDENTITY_INSERT [dbo].[Vendors] OFF

I want to get values based on vendors. Let's say I want to query for Vendor 1 and the query suppose to bring 3 values. Order Id = 12 the first detail, Order Id = 13 both details. But my query below brings just the Order Id = 13.

C#:
      public async Task<IEnumerable<Order>> GetOrdersForExport(int vendorId,string status, DateTime? startDateTime, DateTime? endDateTime)
             {
                    
        
                 IQueryable<Order?> result = _db.Orders.Include(c=>c.Customer).Include(d => d.OrderDetails).ThenInclude(v => v.Vendor);
        
                 if (vendorId != null)
                 {
                     result = result.Where(d => d.OrderDetails.All(s => s.VendorId == vendorId));
                 }
        
                 if (status != null)
                 {
                     result = result.Where(dy => dy.Status == status);
                 }
                    
                 return await result.ToListAsync();
             }

As an example, if I want to query for vendor Id = 1 then I should get;


Get Order Id = 12 and OrderDetail Id = 18


Get Order Id = 13 and OrderDetail Id = 20 and 21
 
That's what you are saying you are expecting to get. Can you show us what you are actually getting?
 
Moving to Entity Framework. My gut feel is that this isn't a LINQ specific issue, but rather how Entity Framework does its queries.
 
Get Order Id = 12 and OrderDetail Id = 18
Why would you expect this result? In your code you are saying:
C#:
result = result.Where(d => d.OrderDetails.All(s => s.VendorId == vendorId));

Order ID 12 has two order details, where one has a vendor id of 1, and the other order detail has a vendor id of 2. The All() condition will not be satisfied.
 
Anyway, this is some code that is a little bit easier to play with. No Entity Framework involved.
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

void DumpOrders(IEnumerable<Order> orders)
{
    foreach(var order in orders)
        Console.WriteLine(order);
}

Console.WriteLine("All:");
DumpOrders(GetOrdersForExport(null, null));

Console.WriteLine();
Console.WriteLine("VendorId == 1:");
DumpOrders(GetOrdersForExport(1, null));


IEnumerable<Order> GetOrdersForExport(int? vendorId, string status)
{
    var orders = Repository.Context.Orders;
    if (vendorId.HasValue)
        orders = orders.Where(o => o.OrderDetails.All(od => od.VendorId == vendorId));
    if (!string.IsNullOrWhiteSpace(status))
        orders = orders.Where(o => o.Status == status);
    return orders;
}

record class Order(
    int Id,
    DateTime OrderDateTime,
    string Status,
    string DoneBy,
    int CustomerId)
{
    public IEnumerable<OrderDetail> OrderDetails => Repository.Context.OrderDetails.Where(od => od.OrderId == Id);

    protected virtual bool PrintMembers(StringBuilder sb)
    {
        sb.AppendLine($"Id = {Id}, OrderDateTime = {OrderDateTime}, Status = {Status}, DoneBy = {DoneBy}, CustomerId = {CustomerId}, ");

        bool first = true;
        sb.Append("OrderDetails = {");
        foreach (var orderDetail in OrderDetails)
        {
            sb.AppendLine(first ? "" : ",");
            sb.Append($"  {orderDetail}");
            first = false;
        }
        sb.Append(" }");
        return true;
    }
}

record class OrderDetail(
    int Id,
    string ProductCode,
    string ProductName,
    int Quantity,
    double CostRatio,
    decimal UnitCost,
    decimal TotalBuyPrice,
    decimal TotalSellPrice,
    string ShippingNumber,
    string Status,
    string TrackingNumber,
    string Description,
    int OrderId,
    int VendorId,
    string Currency,
    decimal BuyUnitPrice,
    decimal SellUnitPrice)
{
    public Vendor Vendor => Repository.Context.Vendors.FirstOrDefault(v => v.Id == VendorId);
}

record class Vendor(
    int Id,
    string Name,
    string Address,
    string Email,
    string PhoneNumber,
    string MainResponsibleName,
    string AssistantResponsibleName);

class Repository
{
    public IEnumerable<Order> Orders { get; init; } = new Order[]
    {
        new (12, DateTime.Parse("2022-08-25 12:09:33"), "Continues", "Cenk", 5),
        new (13, DateTime.Parse("2022-08-28 17:44:46"), "Completed", "Sedat", 6),
    };

    public IEnumerable<OrderDetail> OrderDetails { get; init; } = new OrderDetail[]
    {
        new (18, "123456", "Modem", 10, 12, 0, 0, 0, "ttt", "Getting ready", "1224242", "ffgff", 12, 1, "TL", 3.5m, 4.2m),
        new (19, "444", "Fridge", 50, 18, 0, 0, 0, "y", "At customs", "65yu77", "yyyuuuu", 12, 2, "Dolar", 1.4m, 2.2m),
        new (20, "3333", "home", 12, 13, 0, 0, 0, "ship", "Completed", "63737jkkkk", "dikkat", 13, 1, "Euro", 5, 6.2m),
        new (21, "uyuyeh778", "test", 40, 12, 0, 0, 0, "fddfd", "Completed", "434343", "fgdgdfd", 13, 1, "TL", 8, 11),
    };

    public IEnumerable<Vendor> Vendors { get; init; } = new Vendor[]
    {
        new (1, "Test Vendor1", "Test Address1", "testemail@mail.com", "123456789", "responsible", "responsible2"),
        new (2, "Test vendor2", "Address2", "vendor@vendor.com", "000000000011122", "Main Resp1", "Assitant Resp1"),
        new (3, "Add Names", "duadadajdajda", "mail@mail.comd", "8329392842823", "kdakdla daşd", "dşakdşadad"),
    };

    public static Repository Context { get; private set; } = new Repository();
}
 
Last edited:
This works for me, thank you.
C#:
public async Task<IEnumerable<ReportViewModel>> GetOrdersForExport(int vendorId,string status, DateTime? startDateTime, DateTime? endDateTime, string detailStatus)
        {
            

            IQueryable<ReportViewModel> result = from o in _db.Orders
                join c in _db.Customers on o.CustomerId equals c.Id
                join od in _db.OrdersDetail on o.Id equals od.Order.Id
                join v in _db.Vendors on od.Vendor.Id equals v.Id
                select new ReportViewModel()
                {
                    OrderId = o.Id,
                    OrderDateTime = o.OrderDateTime,
                    CustomerId = o.CustomerId,
                    Status = o.Status,
                    DoneBy = o.DoneBy,
                    OrderDetailId = od.Id,
                    ProductCode = od.ProductCode,
                    ProductName = od.ProductName,
                    Quantity = od.Quantity,
                    BuyUnitPrice = od.BuyUnitPrice,
                    CostRatio = od.CostRatio,
                    UnitCost = od.UnitCost,
                    TotalBuyPrice = od.TotalBuyPrice,
                    SellUnitPrice = od.SellUnitPrice,
                    TotalSellPrice = od.TotalSellPrice,
                    ShippingNumber = od.ShippingNumber,
                    OrderDetailStatus = od.Status,
                    TrackingNumber = od.TrackingNumber,
                    Description = od.Description,
                    Currency = od.Currency,
                    CustomerName = c.Name,
                    VendorName = v.Name,
                    VendorId = v.Id
                };

            if (vendorId > 0)
            {
                result = result.Where(d => d.VendorId == vendorId);
            }

            if (status != null)
            {
                result = result.Where(dy => dy.Status == status);
            }

            if (startDateTime != null)
            {
                result = result.Where(dt =>
                    dt.OrderDateTime >= DateTime.ParseExact(startDateTime.ToString(), "dd.MM.yyyy 00:00:00", null));
            }
            if (endDateTime != null)
            {
                result = result.Where(dt =>
                    dt.OrderDateTime <= DateTime.ParseExact(endDateTime.ToString(), "dd.MM.yyyy 00:00:00", null));
            }

            if (detailStatus != null)
            {
                result = result.Where(dy => dy.OrderDetailStatus == detailStatus);
            }

            return await result.ToListAsync();
        }
 
So sad that Entity Framework, an object relational mapper, can't seem to do a basic set of joins to map the object relationships to relational tables. Our OP had to resort to doing the joins himself.
 
Back
Top Bottom