QUESTION: Is possible to include a filter inside a GetAllIncluding?
See original GitHub issueHi All,
This is not an issue. I am looking for an idea to solve my problem.
Let’s imaging the following case. I have two tables: Customer and ShipTo. The ShipTo table is a child of the Customer.
Customer fields: ID Name Type
ShipTo fields ID Name Address State (numeric) City (numeric) CustomerID
If I want to retrieve all my Customers including their ShipTos I would do something like this:
Repository.GetAllIncluding(x => x.ShipTo)
Considering Repository as the repository for Customer. All this is ok. This would retrieve all my customer including all their ShipTos.
Now, what happens if I would retrieve all my customers and only their ShipTos filter by state and city.
Considering the following records
Customer
ID NAME TYPE 1 ABC C 2 XYZ P 3 DEF C
ShipTo
ID NAME ADDRESS STATE CITY CUSTOMERID 1 A Prometeo 10 5 1 2 B Villa 11 6 1 3 C Dorada 10 5 2 4 D Florencia 10 4 2 5 E Valle 12 5 3
If I want the customers and only their ShipTos for State 10 and City 5, I would expect the following result:
Customer
ID NAME TYPE 1 ABC C 2 XYZ P
ShipTo
ID NAME ADDRESS STATE CITY CUSTOMERID 1 A Prometeo 10 5 1 3 C Dorada 10 5 2
In my REST API for the Customer object, I am running the GetAll action. This triggers the CreateFilteredQuery method in my AppService, inside this method, I added something like this:
protected override IQueryable<Customer> CreateFilteredQuery(PagedCustomerResultRequestDto input)
{
List<int> shipTosListFiltered = new List<int>();
if (!input.State.IsNullOrWhiteSpace() || !input.City.IsNullOrWhiteSpace())
{
shipTosListFiltered = _shipToManager.GetShipTosInCityAndState(input.Town, input.State);
}
return Repository.GetAllIncluding(x => x.ShipTo)
.WhereIf(shipTosListFiltered.Count > 0, pbs => pbs.ShipTo.Any(pb => shipTosListFiltered .Contains(pb.Id)));
}
This retrieves the customer 1 and 2 correctly. But it returns me all their ShipTos and I would like them filtered.
Does someone know if there is a way to filter this as I am expecting?
I would really appreciate an idea for this.
Thanks in advance.
Regards, Arturo
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
I was talking about a simpler query like below;
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.