Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select multiple dates and query in linq #4673

Closed
raysefo opened this issue Mar 14, 2023 · 3 comments
Closed

Select multiple dates and query in linq #4673

raysefo opened this issue Mar 14, 2023 · 3 comments
Labels
Area: Documentation Something is missing in the documentation.
Milestone

Comments

@raysefo
Copy link

raysefo commented Mar 14, 2023

Hi,
I have a Blazor Server Application, I am querying database based on start and end dates as follows.

`await using var ctx = await _db.CreateDbContextAsync();
            IQueryable<ReportViewModel> result = from o in ctx.Orders
                join c in ctx.Customers on o.CustomerId equals c.Id
                join od in ctx.OrdersDetail on o.Id equals od.Order.Id
                join v in ctx.Vendors on od.Vendor.Id equals v.Id
                orderby o.Id descending
                select new ReportViewModel()
                {
                    OrderId = o.Id,
                    OrderDateTime = o.OrderDateTime.Date,
                    CustomerId = o.CustomerId,
                    Status = o.Status,
                    DoneBy = o.DoneBy,
                    OrderDetailId = od.Id
                };
          
            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));
            }`

I wonder how can I select multiple dates (date picker) and use it in my query?
Thanks in advance.

@David-Moreira
Copy link
Contributor

Hello @raysefo
You may use one of the examples for multiple dates, either the Range one or the Multiple whichever fits your needs best.
As for picking up the multiple dates, there's a parameter and respective eventcallback for binding the multiple dates, called Dates.
You can use either

  • @bind-Dates="Dates"
  • or just Dates="Dates" DatesChanged="DatesChanged"
    According to your needs.

Granted the Dates api is missing from the docs, we'll update them accordingly.
Let us know if this helps you.

@raysefo
Copy link
Author

raysefo commented Mar 15, 2023

Hi @David-Moreira
Thank you for your reply. In either case, @bind-Dates="Dates" or Dates="Dates" DatesChanged="DatesChanged" I should change my query, right? What does Dates return? Selected dates with a comma between?

@David-Moreira
Copy link
Contributor

Hello @raysefo I didn't quite look at your query since that's your job... :) But from a shallow look seems fine? You might want to query the dates in the EF Core query itself, so it already brings the data from the database filtered by dates instead of bringing the data to memory and only then filtering by the dates.

As for the Dates Parameter, it's a List of Dates, IReadOnlyList<DateTime?> as you'd expect.
If you are not able to get it to work, just post the code that you are trying to use here, we'll help.

@stsrki stsrki added the Area: Documentation Something is missing in the documentation. label Mar 15, 2023
@stsrki stsrki added this to the 1.2 support milestone Mar 15, 2023
@stsrki stsrki closed this as completed Mar 15, 2023
@stsrki stsrki added this to Support Aug 3, 2024
@stsrki stsrki moved this to ✔ Done in Support Aug 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Documentation Something is missing in the documentation.
Projects
Archived in project
Development

No branches or pull requests

3 participants