Get the the most recent and the one before the most recent item - linq

I have a table with many anniversaries : Date + Name.
I want to display the next anniversary and the one after with Linq.
How can i build the query ?
I use EF
Thanks
John

Just order by date and then use the .Take(n) functionality

Example with a list of some objects assuming you want to order by Date then Name:
List<Anniversaries> annivDates = GetAnnivDates();
List<Anniversaries> recentAnniv = annivDates.OrderBy(d => d.Date).ThenBy(d => d.Name).Take(2).ToList();

If the anniversaries are stored in regular DateTime structs, they may have the 'wrong' year set (i.e. wedding or birth year). I suggest writing a function which calculates the next date for an anniversary (based on the current day) like:
static DateTime CalcNext(DateTime anniversary) {
DateTime newDate = new DateTime(DateTime.Now.Year, anniversary.Month, anniversary.Day);
if (newDate < DateTime.Now.Date)
newDate = newDate.AddYear(1);
return newDate;
}
Then you proceed with sorting the dates and taking the first two values like described in the other postings:
(from e in anniversaries orderby CalcNext(e.Date) select e).Take(2)

Related

List of the dates between two dates from another table

I created the list of the dates between two dates (minimum and maximum) from another table where i compare the max date and today.
let
first = List.Min(#"Mizan (Banka&Kasa)"[Tarih]),
last = List.Max(
{(List.Max(#"Mizan (Banka&Kasa)"[Tarih])), Date.From(DateTime.LocalNow())}
),
howmanydays = Number.From(last) - Number.From(first) + 1,
Source = List.Dates(first, howmanydays, #duration(1, 0, 0, 0))
in
Source
But I am sure there must be an easier way to achieve this list.
Any ideas ?
Thats pretty much it, unless you prefer to omit howmanydays and use
Source = {Number.From(first) .. Number.From(last)}
then expand and convert format to date
If you want to create a table with the dates of another table maybe the best is:
CREATE NEW TABLE
date = CALENDAR(maxx(table_name, table_name[Date]),TODAY())
table_name is the table you want to select
table_name[Date] is the cell you want to get the last date

Retrieve database records between two weekdays

I have several records in my database, the table has a column named "weekday" where I store a weekday like "mon" or "fri". Now from the frontend when a user does search the parameters posted to the server are startday and endDay.
Now I would like to retrieve all records between startDay and endDay. We can assume startDay is "mon" and endDay is "sun". I do not currently know how to do this.
Create another table with the names of the days and their corresponding number. Then you'd just need to join up your current table with the days table by name, and then use the numbers in that table to do your queries.
Not exactly practical, but it is possible to convert sun,mon,tue to numbers using MySQL.
Setup a static year and week number like 201610 for the 10th week of this year, then use a combination of DATE_FORMAT with STR_TO_DATE:
DATE_FORMAT(STR_TO_DATE('201610 mon', '%X%V %a'), '%w')
DATE_FORMAT(STR_TO_DATE('201610 sun', '%X%V %a'), '%w')
DATE_FORMAT(STR_TO_DATE('201610 tue', '%X%V %a'), '%w')
These 3 statements will evaluate to 0,1,2 respectively.
The main thing this is doing is converting the %a format (Sun-Sat) to the %w format (0-6)
well i don't know the architecture of your application as i think storing and querying a week day string is not appropriate, but i can tell you a work around this.
make a helper function which return you an array of weekdays in the range i-e
function getWeekDaysArray($startWeekDay, $endWeekDay) {
returns $daysArray['mon','tue','wed'];
}
$daysRangeArray = getWeekDaysArray('mon', 'wed');
now with this array you can query in table
DB::table('TableName')->whereIn('week_day', $daysRangeArray)->get();
Hope this help

Linq Get 7 days prior to given date

Is it possible to get all 7 days for a given date using linq query, please?
I could use the other methods to populate the list but thought would be wise to use 2 lines of LINQ if possible.
Thanks
Hacky but
IEnumerable<DateTime> getDays(DateTime date) {
return Enumerable.Range(0, 7).Select(n => date.AddDays(-1 * n));
}
Note: didn't test and not positive about the date methods.

How can I compare 2 dates in a Where statement while using NoRM for MongoDB on C#?

I have a table in Mongo. One of the fields is a DateTime. I would like to be able to get all of the records that are only for a single day (i.e. 9/3/2011).
If I do something like this:
var list = (from c in col
where c.PublishDate == DateTime.Now
select c).ToList();
Then it doesn't work because it is using the time in the comparison. Normally I would just compare the ToShortDateString() but NoRM does not allow me to use this.
Thoughts?
David
The best way to handle this is normally to calculate the start datetime and end datetime for the date in question and then query for values in that range.
var start = DateTime.Now.Date;
var end = start.AddDays(1);
...
But you'd also be well advised to switch to the official C# driver now. You should also use UTC datetimes in your database (but that gets more complicated).

Combining all dates, and data, within a month!

I am trying to combine all days of each month into a date.
My query as off now:
SELECT
inventory_items.acquired_at AS Date_Acquired,
products.name AS products_name,
SUM(inventory_items.primary_quantity) AS inventory_items_primary_quantity
FROM
inventory_items inventory_items INNER JOIN customers customers ON inventory_items.source_id = customers.id
INNER JOIN products products ON inventory_items.product_id = products.id
GROUP BY
MONTH(Date_Acquired),
products_name
ORDER BY
MONTH(Date_Acquired)
I have a general idea of what to do, but not really sure how to implement it.
As I understand you and your Date_Acquired is an instance of sql Date type
you can gat day of months as pasting below code inside a textfield
(new SimpleDateFormat("d")).format(new java.util.Date())
which suppose to give you numbers like 1,2,3,...18,19...
Extra:
(new SimpleDateFormat("M")).format(new java.util.Date()) for month
(new SimpleDateFormat("yyyy")).format(new java.util.Date()) for year
(new SimpleDateFormat("d")).format(new java.util.Date())+" - "
+(new SimpleDateFormat("M")).format(new java.util.Date()) for getting a value like 28 - 01
What database? A typical SQL database result can only contain one data value per field. So you will not be able to retrieve all the products.name values in one result grouped by the month. If you retrieve all the results under a specified month you can aggregate them later on.

Resources