Home > Software design >  Selection from the table by two parameters
Selection from the table by two parameters

Time:07-28

There is a table, where i store information about bookings. It's a simple one. Has just few attr like: bookingId, personId, date and shouldBeConsidered

I want to retrieve for each person booked from som period of time for last 10 days and then to include property shouldBeConsidered.

For instance there were 100 bookings and 2 of those amount should be taken from db. And also, there can be person who booked 100 booking where is 0 shouldBeConsidered

I've code something like this:

var res = await this.context.Bookings
                    .Where(x => x.DateTime >= bookingsStartDateTime || x.ShouldBeConsidered)
                    .GroupBy(x => br.PId)
                    .Select(x => Dal
                    {
                        Id = br.Key,
                        TotalBookings = x.Count(),
                        BookingsIssues = x.Count(x => x.ShouldBeConsidered)
                    })
                    .ToListAsync();

But, unfortuanlly, it does not work properly. Sometimes, it could take a bit more issues and also count a bit more total.

CodePudding user response:

You will need a bit more clear example with a few record scenarios followed by what you expect to have been returned and what you actually got returned. Your logic is going to give you all bookings where either the booking date >= that start date OR the ShouldBeConsidered flag is true. (regardless of date)

On a hunch I think you probably want to remove that || x.ShouldBeConsidered in that ultimately you'd be interested in the total # of bookings after the start date, and a separate count of the bookings after the start date with that flag set. the statement "and then to include property shouldBeConsidered" seems confusing. All properties of the booking are included/available for querying against:

var res = await this.context.Bookings
    .Where(x => x.DateTime >= bookingsStartDateTime)
    .GroupBy(x => br.PId)
    .Select(x => Dal
    {
        Id = br.Key,
        TotalBookings = x.Count(),
        BookingsIssues = x.Count(x => x.ShouldBeConsidered)
    }).ToListAsync();
  • Related