'How do get all customers who haven't placed an order in 5 days in LINQ [closed]
This is a different version of what I need but made so that I don't break NDA. The code for what I need should be pretty similar. The database would essentially be a customer table(first name, last name, phone number, customer id, etc...), an order table(product name, product id, product cost, etc... this one doesn't matter as much), and a middle table that would hold the details for each purchase(customer id, product id, date bought, etc...). The code I've tried goes as follows.
var users = _context.Customers
.Include(c => c.orders)
.Where(c => c.orders == 0)
.Where(o => o.orders
.OrderBy(o => o.DateBought)
.Last().DateCreated <= DateTime.Now.AddDays(-5))
.AsNoTracking()
.ToListAsync();
I can't really think of anything else and any help would be appreciated. This is for my job, and my boss really doesn't help me out so I can't really ask him. It is important to note that I'm not allowed to use joins(LINQ joins). I'm not sure why, but he is really insistent that it has to be with .include and nothing else. I do not need anything other than the customer.
Thank you!
Solution 1:[1]
I would try simply using Any on orders with corresponding predicate (though not sure if your ORM and database provider will be able to translate this):
var users = _context.Customers
.Where(c => !c.orders.Any(o => o.DateCreated > DateTime.Now.AddDays(-5))
.AsNoTracking()
.ToListAsync();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Guru Stron |
