Reputation: 4067
I have a service tax calculation in my page.For that i have to get the current service tax.
Service Tax table is as follows
Date Percentage
2015-10-01 00:00:00.000 14
2015-11-15 06:12:31.687 14.5
Say if the current date is less than 2015-11-15
I will get the the value of percentage
as 14
and if the current date is equal to or greater than 2015-11-15
i should get the value of percentage
as 14.5
.
How can I implement this using Linq
??
Upvotes: 0
Views: 401
Reputation: 14498
If you need to compare a date, it's better to do so on the database to prevent fetching useless data. To do this you can make use of System.Data.Entity
namespace to access some functions:
db.Taxes.Where(t => DbFunctions.TruncateTime(t.Date)
< DbFunctions.TruncateTime(dateParameter)).FirstOrDefault();
System.Data.Entity.DbFunctions.TruncateTime(Datetime)
trims the time part of a DateTime
value on the database.
Upvotes: 1
Reputation: 931
You need to get all taxes which are lower and fetch only first after sorting:
Taxes
.Where(t => t.Date < DateTime.Now)
.OrderByDescending(t => t.Date)
.First()
Upvotes: 2