how to retrieve data between two dates in linq - asp.net-mvc

I have a datetime column in a table and I want to retrieve data month wise from this date time column. I want to populate a dropdown down list with month name only so that user can search data by month name.
here is my linq code
var filterdate = from dates in _db.entries
where date1 >= dates.PaidOn
where date2 <= dates.PaidOn
select dates;
return View(filterdate);
and how to write the razor view code. please help. I am stuck. I would really appreciate your help. thanks in advance. Gitu

Try following:
var filterdate = from dates in _db.entries
where date1 >= dates.PaidOn
&& date2 <= dates.PaidOn
select dates.PaidOn.Month;
or
var filterdate = _db.entries.Where(i=> date1 >=i.PaidOn && date2 <= i.PaidOn )
.Select(i=>i.PaidOn.Month);

Related

Parse query where key contains A or B - Swift

I am trying to run a parse query where a certain field contains either todays date or tomorrows date. Here is the code I have so far:
var date = Date() // first date
let calendar = Calendar.current
let date2 = (calendar as NSCalendar).date(byAdding: .day, value: 7, to: date, options: [])!
let dateFormatter:DateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let todaysString:String = dateFormatter.string(from: date)
let tomorrowString:String = dateFormatter.string(from: date2)
let query = PFQuery(className: "Events")
query.whereKey("dateString", contains: todaysString || tomorrowString)
But this will not run because it cannot convert the value. Does anyone know how I am suppose to write this line to show that dateString needs to contain either todaysString or tomorrowString? Thanks in advance.
You need to create separate queries, and join them with PFQuery.orQueryWithSubqueries(todayQuery, tomorrowQuery).
You need to create two separate query, Q1for todaysString and Q2 for tomorrowString. Now add both queries in an Array. And use PFQuery's function "orQueryWithSubqueries" to merge them in 3rd query Q3 and then execute the 3rd query Q3. I have used this in objective c and worked for me, I can post objective c code if you want

get entries by date component in core data

I want to filter entries from my database by single date components such as month=12. Notice that I can't query for entries with dates between to fixed dates because the year might be different.
It should look/work like this:
let request = NSPredicate(format: "date.month=%i", 12)
You will have to use a time interval for the predicate.
NSPredicate(format: "date >= %# && date < %#", aDate, aDate.date(byAddingMonths:1))
Please note that you perhaps mixed up the NSFetchRequest with the NSPredicate in your question.
To add a month:
extension Date {
func date(byAddingMonths months: Int) -> Date {
var diff = DateComponents()
diff.month = months
return Calendar.current.date(byAdding: diff, to: self)!
}
}
EDIT
After clarifying the question, it appears you need a month attribute. Add an Int attribute to your entity that should be filled out with the month automatically whenever you change the date (override didSet). Here is how you get the month from the date:
let month = Calendar.current.component(.month, from:theDate)

Trying to return ONLY tomorrow's data using Swift and Core Data but it returns both today's and tomorrow's together?

I am trying to return ONLY tomorrow's data using Swift and Core Data but it returns both today's and tomorrow's together. Any idea why? Below is the code I am using. Thank you in advance!
let today = NSDate()
let tomorrow = NSCalendar.currentCalendar()
.dateByAddingUnit(
.Day,
value: 1,
toDate: today,
options: []
)
todoTomorrow = CoreDataManager.getData("ToDos", predicate: NSPredicate(format:"dueDate<%#", ((tomorrow))!)) as! [ToDos]
You need to modify your predicate to be dueDate < %# AND dueDate > %# with parameters tomorrow and today so it checks both the upper and lower allowed range of the dates.

Swift Core Data Fetch results from a certain date

I'm fairly new to ioS dev so this might be obvious.
With Core Data, I have an Entity : 'Post' , with an attribute for "dateAdded".
When the user selects an NSDate on a calendar- I want to fetch all the entries on that day only and load that into a table.
I was thinking of using a predicate where dateAdded (NSDate) would be equal to the calendarSelectedNSDate. But I don't think that would work as their NSDates would be different because of different times.
I would really appreciate a solution! Thank you!
Use (NS)Calendar to calculate the start and end of the selected date and create a predicate:
// get the current calendar
let calendar = Calendar.current
// get the start of the day of the selected date
let startDate = calendar.startOfDay(for: calendarSelectedNSDate)
// get the start of the day after the selected date
let endDate = calendar.date(byAdding: .day, value: 1, to: startDate, options: .matchNextTime)!
// create a predicate to filter between start date and end date
let predicate = NSPredicate(format: "dateAdded >= %# AND dateAdded < %#", startDate as NSDate, endDate as NSDate)

Filter date by number of days with Core Data

I have an entity in my core data database with a field startDate, and a field everyX that stores how often the entity would be active (so if everyX is 2, it would be active every two days). I want to fetch all entities where the number of days between startDate and the current date is dividable by everyX. Figuring out if the number of days between startDate and today should be possible using a modulo with everyX, but I can't find a way to get the number of days.
For example these should be fetched:
startDate = 12.9.15
everyX = 2
today = 14.9.15
startDate = 10.9.15
everyX = 2
today = 14.9.15
These should not be fetched:
startDate = 9.9.15
everyX = 2
today = 14.9.15
startDate = 10.9.15
everyX = 3
today = 14.9.15
NSPredicate does support custom functions, but these do not work when fetching from a core data sqlite storage. So I would need another way to calculate the number of days between two dates that works when using a NSFetchRequest.
I know it would be possible to filter the entities using code after the fetch request, but I'd like to prevent that as I would loose a lot of the benefits of the NSFetchedResultsController I am using
You can try to fetch the data and use dates such as:
var formatter:NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "DD"
var dayNumber1 = formatter.dateFromString(Your first Date)!
var dayNumber2 = formatter.dateFromString(Your second Date)!
var difference = dayNumber2 - dayNumber1
Or you should set your day numbers to another field in the CoreData and you can do operations with that field.

Resources