c# linq group by fortnight or x number of days? - asp.net

Hi I'm trying to work out how would you group items into the first and 2nd half of each month for a given year and month?
i.e.
Say for example I have to list the name of item on the 6th of every month and the 15th of every month.
say for example I have
Flight Name Flight Date
Flight 1 01/07/2012
Flight 2 12/07/2012
Flight 3 18/07/2012
Flight 4 28/07/2012
how would i split it up so I'd like to group flights by fortnights within a year/month
i.e
Flights For July week 1 and 2 - 2012
Flights for July week 3 and 4 - 2012
so this is what I have so far..
eventually it'll have to be some kind of view model using automapper etc i am not sure as of yet how it'd get that neatly into some kind of ViewModel form...
var flightEntities = from f in flightsAsViewModels
select new
{
YearGroups = from flightYearGroup in context.Flights
group flightYearGroup by flightYearGroup.FlightDateTime.Year
into yearGroup
orderby yearGroup.Key descending
select new
{
Year = yearGroup.Key,
MonthGroups = from flightMonthGroup in yearGroup
group flightMonthGroup by flightMonthGroup.FlightDateTime.Month
into monthGroup
orderby monthGroup.Key ascending
select new {
Month = monthGroup.Key,
HalfMonthGroups = from months in monthGroup
group months by (months.FlightDateTime.Day <= 15 ? 1 : 2) into splitMonthFlights
orderby splitMonthFlights.Key
select new { WhichHalfOfMonth = splitMonthFlights.Key, Flights = splitMonthFlights }
}
}
};

I hereby pasted Linq code for the requirement. But not sure, this still can be achievable in shortest way. Please let me know if so.
var monthGroupedDates = from d in dates
group d by d.Month into fd
select new { Month = fd.Key, Dates = fd };
foreach (var g in monthGroupedDates)
{
var groupByHalf = from n in g.Dates
group n by (n.Day <= 15 ? 1 : 2) into gd
orderby gd.Key
select new { WhichHalf = gd.Key, Dates = gd };
foreach (var gh in groupByHalf)
{
string printString = (gh.WhichHalf == 1 ? "First" : "Second") + " week dates are:";
foreach (var h in gh.Dates)
{
printString += h.ToString("dd/MM/yyyy") + ";";
}
Console.WriteLine(printString);
}
}
Please note, dates are one which is mentioned in your requirement. If its Linq with SQL, this may need slight alterations.
Raj

Related

Add data from filter to a variable in power bi

I have created graph with cumulative churn in current month and previos month. I created 2 columns:
CurrentMonth and PrevMonth with values "Yes"/"No" and then I filtered data by these columns.
CurrentMonth =
var currentrowyearmonth = FORMAT('Sheet1 (2)'[datetime]; "yyyymm")
var istoday = FORMAT(MAX('Sheet1 (2)'[datetime]); "yyyymm")
return if(istoday = currentrowyearmonth; "Yes"; "No")
PrevMonth =
var currentrowyearmonth = FORMAT(('Sheet1 (2)'[datetime]); "yyyymm")
var istoday = FORMAT(EDATE(MAX('Sheet1 (2)'[datetime]); -1); "yyyymm")
var currentrowday = DAY('Sheet1 (2)'[datetime])
var maxday = DAY(MAX('Sheet1 (2)'[datetime]))
return if(istoday = currentrowyearmonth; if(currentrowday <= maxday; "Yes"; "No"); "No")
I want to be able plot the same graph for any date selected in the filter.
For example,
If I chose today's date June will be the current month, and May the previous month. If I chose May 26 May will be the current month, and April the previous month and then the graph is automatically rebuilt.
Later date strings should not be counted.
I need to replace MAX('Sheet1 (2)'[datetime]) in var "istoday" to selected date from filter.
How can I do this or does this task require something else?

How To count age from date of birth

I am using moment.js with angular, how to get age in years, month and days. here is my code:
getAge(date) {
let age = moment().diff(date, 'year');
return age;
}
but it can give age only by year. How to get the age in years,days and month?
var m1 = moment();
var m2 = moment(DATE OF BIRTH,'YYYY-MM-DD HH:mm:ss');
var diff = moment.preciseDiff(m1, m2); // '1 month 2 days 3 hours 4 minutes 5 seconds'
This requires moment-precise-range.js to be included. For a detailed example, check this here - https://codebox.org.uk/pages/moment-date-range-plugin

Find the number of week rows in a month

Given the start day (Wednesday = 4), and the number of days in a month (31), what is an elegant way to find the number of week rows a calendar of the month would require?
For the current month (startDay = 4, daysInMonth = 31), it would be 5. But if daysInMonth = 33, it would be 6 rows.
This doesn't quite work:
int numRows = (startDay+daysInMonth)/daysInWeek;
if ((startDay+daysInMonth) % daysInWeek != 0) {
numRows++;
}
Just change to
int numRows = (startDay + daysInMonth - 1) / daysInWeek;
if ((startDay+daysInMonth - 1) % daysInWeek != 0) {
numRows++;
}
and you should get the correct result.
EDIT: just to slightly expand on it : you have the right idea, you just forgot to account for the fact that the offset for day 1 is 0, not 1.
Actually, I think your original algorithm is correct, just need to subtract 1 when doing modulo daysInWeek.
daysInWeek = 7
startDay = 3 # Zero based day of week array, 3 = Wednesday
daysInMonth = 31
numRows = (startDay+daysInMonth)/daysInWeek
if ((startDay+daysInMonth - 1) % daysInWeek != 0)
numRows += 1
end
print numRows
It shows 6 correctly. (BTW, why do you need a month with 33 days?) It should be 6 rows for a 33 day month (if there was such a thing).
int temp = daysInMonth;
temp = temp - (7 - startDay);
int result = ceiling(temp / 7) + 1;
Here is a generic way to do it in C#, which works by counting the Saturdays in a month and then adding one if there's any left over days. Since it literally reads a calendar like a human would, there's no strange calendar arithmetic needed. It's all offloaded to the C# DateTime code, we just piggyback off that.
I chose Saturday because most calendars go Sunday (far left) to Saturday (far right). You can just choose a different day if you wish to denote the end of a week.
public static int RowsForMonth(int year, int month)
{
int days = DateTime.DaysInMonth(year, month);
int rows = 0;
int i = 0;
while(i < days)
{
i++;
DateTime date = new DateTime(year, month, i);
if (date.DayOfWeek == DayOfWeek.Saturday || i == days)
rows++;
}
return rows;
}
Jan 2022 -> 6
Feb 2022 -> 5
Mar 2022 -> 5

Given a date how to get Sunday & Saturday of that week

I want to get the Sunday & Saturday of the week from which a date is provided.
I have access to the following functions only:
getDate() returns a number from 0-6 (0 being sunday)
getDay() returns a number from 1-31
getMonth() returns a number from 0-11
getFullYear() returns the current year
I am doing this on titanium.
Per your description above, I came up with:
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDate() + getDay());
var sun = new Date(input.getFullYear(), input.getMonth(), getDay() + (input.getDate() - 6));
If I follow the MDN doc, I come up with (works in Ti too):
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDay() + input.getDate());
var sun = new Date(input.getFullYear(), input.getMonth(), input.getDate() + (input.getDay() - 6));
Where input is a javascript Date object.
The date object will take care or changing the month and/or year if necessary.
Hope this helps.

How Can I add date to 1 year. in X++

How Can I add date to 1 year. I have field with date. now i want to add another field which add 1 year to previous field (date) in X++ code
Ex: 19/10/2010 to 18/10/2011
Maybe:
TransDate dt = 19\10\2010;
info(strfmt("date is %1",nextYr(dt)));
You want the date before the same date next year:
nextYr(19\08\2011 - 1)
The function nextYr gives you the same date, so you have to subtract by 1.
TransDate dt = 19\10\2010;
TransDate dt_res = mkdate(dayofmth(dt), mthofyr(dt), year(dt) + 1 ) - 1;
I got an Answer ..
Create a display method on the table
Diplay Date m1()
{
date d;
d = this.fieldDate + 364;
return d;
}
just drag & drop it on the form design (grid, Group -like)
exetue it 1 year will be added to previous Date

Resources