I want to query my item in table Items, where the last update of each item must be less than 91 days old (from last update till now) and the quantity > 0.
This is my code in the Model:
public IList<Item> GetAllProducts()
{
var ien_item = from i in this.DataContext.Items
orderby i.LastUpdated descending
select i;
return ien_item.ToList().Where(
s =>
HelperClasses.HelperClass.IsLastUpdate(s.LastUpdated.Value) == true
&&
(s => s.Quantity) > 0
)
.ToList();
}
Anyone can solve it? Thanks.
We don’t really know what’s not working here. EDIT: Merlyn spotted it; your lambda syntax is messed up. There’s more to do here though.
However, I’d have thought you’d want this:
Note that this is able to do all the querying at the database side instead of fetching all the items and filtering at the client side. It does assume that
HelperClasses.HelperClass.IsLastUpdateis simple though, and basically equivalent to the filter I’ve got above.(One additional point to note is that by evaluating
UtcNow.Dateonce, the result will be consistent for all items – whereas if your code evaluates “today” on every call toIsLastUpdate, some values in the query may end up being filtered against a different date to other values, due to time progressing while the query is evaluating.)EDIT: If you really need to use
HelperClasses.HelperClass.IsLastUpdatethen I’d suggest:… then at least the quantity filter is performed at the database side, and you’re not creating a complete buffered list before you need to (note the single call to
ToList).