I'm having an issue with retreiving the correct data set from my database. And my knowledge about it is not enough to get it fixed.
I’m creating an application for my uncle his transport company. So he has drivers who drive an assignment and for each assignment the driver drives multiple rides. So it looks like: One driver has zero or multiple assignments but an assignment is for one driver only and an assignment has multiple drives but a drive is for one assignment only.
What I’m trying to do is to get all drives of a driver from a month month because I need to export this to an Excel and I want to create a monthly overview in the app. This is what I did to get all the drives from a driver all time:
public Chauffeur FindAlleOpdrachtenRitten(string id)
{
return _ctx.Users
.Include(o => o.Opdrachten)
.Include(o => o.Opdrachten.Select(x => x.Ritten))
// .Where(o => o.Opdrachten.(r => r.Datum == DateTime.Now))
.SingleOrDefault(x => (x.Id == id));
}
Or to put it in a (my)SQL statement it should look like this:
SELECT * FROM ICS.AspNetUsers as a
INNER JOIN ICS.Opdrachts as o on a.Id = o.ChauffeurID
INNER JOIN ICS.Rits as r on o.OpdrachtID = r.OpdrachtID
WHERE a.UserName = "dimitrivantillo@gmail.com"
AND r.Datum > "1-12-2016";
The code snippet allready shows something I've allready tried and does not work (and the DateTime.Now was just to try something). So I'm looking for some guidance on how to solve this. Am I looking in the correct direction? I found some other topics with more or less the same problem but not close enough to my situation.
The export to excel function is working but my ultimate goal is to save the database by not querying for all the table rows and only query for a specific month. I’ve been thinking of just getting all the rows and filter the specific month afterwards but wouldn’t that consume a lot of resources in the end? (after a few years).
Also note that I'm Dutch speaking. Chauffeur = drivers, Opdracht = Assignment and Rit = drive
Thanks for reading and hopefully helping me:D
Edit: the includes themselves are working, I get all the necessary data but not the filtered related object. So all drives instead of the drives for a specific month because the where function does not work