I previously asked this question here: List of Dates ordered in a certain way
I thought the proposed solution was fine until the year ticked over on my date list and encountered an issue.
My date list (in this string based format - this is how the data comes to me from the source API)
201711
201712
201801
201811
201812
201901
I want to present my data in a bar chart to show 3 months worth of year on year comparison in month order. This would mean I order the list as so
201711
201811
201712
201812
201801
201901
So I can then see year-on-year bars for November, December and Jan in that order.
I've tried the solution at the bottom of the question but it places the order like so (which is not what I want):
201801
201901
201711
201811
201712
201812
For clarity, next month it will need to move forward to be this date list:
the first month I want will always be 2 months before the current one
201712
201812
201801
201901
201802
201902
var rowsInOrder = dateList.OrderBy(n => DateTime.ParseExact(n, "yyyyMM", CultureInfo.CurrentCulture).Month).ThenBy(n=> DateTime.ParseExact(n, "yyyyMM", CultureInfo.CurrentCulture).Year);
You can use this Lookup approach that first determines the month-groups:
var monthLookup = dateList
.Select(s => new{String = s, Date = DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture)})
.OrderBy(x => x.Date) // not necessary in your sample data but i assume it's desired
.ToLookup(x=> x.Date.Month);
var rowsInOrder = monthLookup.SelectMany(x => x).Select(x => x.String);
I could manage to achieve your goal using GroupBy to group months,
var rowsInOrder = new List<string>();
foreach (var grouping in dates.GroupBy(s =>
DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture).Month))
{
rowsInOrder.AddRange(grouping.OrderBy(s => s));
};
You can also order months with same logic:
var rowsInOrder = new List<string>();
foreach (var grouping in dates
.OrderBy(s => DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture).Month).GroupBy(s =>
DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture).Month))
{
rowsInOrder.AddRange(grouping.OrderBy(s => s));
}
It seems to me that this is sufficient:
var rowsInOrder = dates.OrderBy(x => x).GroupBy(x => x.Substring(4)).SelectMany(x => x);
There's simply no need to muck around with parsing dates. It's a simple string sort and group this way.
So you are stuck with a sequence of objects, where every object has a Date property of type string in the format yyyyMM. and you want to extract some data from it.
Your date format is language independent. It doesn't matter whether your computer is a British one, or a Chinese one. The Date property will always be in format yyyyMM.
This makes it fairly easy to convert it into a DateTime format, which makes it easy to access the Year and Month.
const string dateTimeFormat = "yyyyMM";
CultureInfo provider = CultureInfo.InvariantCulture;
var dateList = ... // your original list of items with the string Date property
var itemsWithYearMonth = dateList.Select(item => new
{
DateTime = DateTime.ParseExact(item, dateTimeFormat, provider)
... // select other items you need for your bar chart
});
Now, given a StartYear/Month, a NrOfYears and a NrOfMonths you want to group dateTimeItems into groups of same month.
For example, starting at 2018-11, I want groups with four months, for three consecutive years (yeah, yeah, I know in your original request it was only 3 months, 2 years, but why limit yourself to this, let's make your code re-usable):
group 1: 2018-11, 2019-11, 2020-11, 2021-11
group 2: 2018-12, 2019-12, 2020-12, 2021-12
group 3: 2019-01, 2020-01, 2021-01, 2022-01
Bonus-points: we'll pass the year boundary!
So input:
var itemsWithYearMonth = ... // see above
int startYear = ...
int startMonth = ...
int nrOfMonths = ...
int nrOfYears = ...
We will make groups of items with same months. We don't want all Months of the year, we only want some months. If we want the 3 months starting at month 11, we need to keep the groups with Months 11, 12, 1.
var desiredMonths = Enumerable.Range(startMonth, nrOfMonths) // example: 11, 12, 13
.Select(monthNr => 1 + ((monthNr-1) % 12)); // 11, 12, 1
From your input, we don't want all months, we only want the year/month larger than the starting month.
DateTime startMonth = new DateTime(startYear, startMonth, 1);
The easiest way is to keep only the input source of items with date equal or larger than startMonth and take only the first NumberOfYears items of each group. This way you get the correct number of items if you pass the year boundary like I did in my example.
var result = itemsWithYearMonth
// keep only the items newer than startMonth
.Where(item => item.DateTime >= startMonth)
// group by same month:
.GroupBy(item => item.DateTime.Month,
(month, itemsWithThisMonth) => new
{
Month = month, // in my example: 11, 12, 1, ...
// in every group: take the first nrOfYears items:
Items = itemsWithThisMonth
// order by ascending year
.OrderBy(itemWithThisMonth => itemWithThisMonth.Year)
// no need to order by Month, all Months are equal in this group
.Take(nrOfYears)
.ToList(),
})
// keep only the desired months:
.Select(group => desiredMonth.Contains(group.Month));
So now you have groups:
group of month 11, with data of years 2018, 2019, 2020, 2021
group of month 12, with data of years 2018, 2019, 2020, 2021
group of month 01, with data of years 2019, 2020, 2021, 2022
Related
I am currently working with real-estate data, and each Listing entity has a ListingDate and a CloseDate. What I am currently trying to do is to count how many Listings are active in a given month and year (group by year and month).
So for an example if Listing1 had an ListingDate of 05/01/2020 and a CloseDate of 08/01/2020, there would be 1 Active count for May, June, July, and August and a year total of 4.
I am using EF and LINQ, and was wondering if I could solve it somehow.
Any help or advice is appreciated.
Sure you can; if you map listings to each month in which it's active, you can then simply group the results by month and get the counts trivially. Thus, the trickiest part is to just come up with the month DateTime values, which isn't that tricky.
Extension method to get month DateTimes from a start and end date:
public static IEnumerable<DateTime> GetMonths(this DateTime startDate, DateTime endDate)
{
var monthDiff = (endDate.Month - startDate.Month) + (12 * (endDate.Year - startDate.Year));
var startMonth = new DateTime(startDate.Year, startDate.Month, 1);
return Enumerable.Range(0, monthDiff + 1)
.Select(i => startMonth.AddMonths(i));
}
Create lookup:
var listingsByMonth = listings
.SelectMany(l =>
{
return l.ListingDate.GetMonths(l.ClosingDate.AddDays(-1)) // assuming closing date is exclusive
.Select(dt => new KeyValuePair<DateTime, Listing>(dt, l));
})
.ToLookup(kvp => kvp.Key, kvp => kvp.Value);
Demonstration of results:
foreach(var g in listingsByMonth)
{
Console.WriteLine($"{g.Key:yyyy-MM}: {g.Count()}");
}
Fiddle
Let's assume that date is given in DateTime structs. (You can parse text input to DateTime, check this) We can iterate over a List containing Listing entities, and perform a check to see if given date is in the range of ListingDate and ClosingDate. If the check succeeds, copy the entity to another list.
DateTime query = ...;
List<Listing> list = ...;
List<Listing> pass = new();
foreach (Listing entity in list)
{
if (entity.ListingTime < query && query < entity.ClosingTime)
pass.Add(entity)
}
While checking whether the query is in range, we could've used DateTime.Compare() but less than/greater than operators make the statement easier to read.
I'm selecting an IEnumerable of DateTime by using a list of int which represent a year.
Each of the resulting DateTimes is given a default month and day of 1 so e.g.
List<int> years = new List<int>() { 2018, 2017, 2016 };
var dateTimes = years.Select(x => new DateTime(int.Parse(x), 1, 1));
Gives me 2018/01/01, 2017/01/01, 2016/01/01 (ignoring the time component)
I want to get the same results but for each month of each year too, so actually 36 results for three given years:
2018/01/01
2018/02/01
...
2018/11/01
2018/12/01
2017/01/01
...
2016/12/01
(this is using non-US culture datetime where months are the middle value)
I was wondering if C# had a really nice shortcut like the range notation
var dateTimes = years.Select(x => new DateTime(int.Parse(x), 1..12, 1));
But that certainly doesn't work.
Any syntactic-shortcut way to achieve this without just looping i = 1 to 12 style?
The best I came up with was:
var dateTimes = new List<DateTime>();
foreach(var y in years)
{
for (int i = 1; i <= 12; i++) dateTimes.Add(new DateTime(int.Parse(y), i, 1));
}
Which does exactly what I want but though there was a more succinct way to write.
var dateTimes = years.SelectMany(y => Enumerable.Range(1, 12).Select(m => new DateTime(y, m, 1))); // .ToList() if you want
Enumerable.Range() available from .NET 3.5
I have a list of DateTime values and I want to split the whole list into sublists for each week.
The dates might span across multiple years(the user selects the start and end date), so a solution that splits them up per calendar week number would end up in conflicts.
Is there a way to traverse the list and then store each week's DateTime values in a new 2d list?
The values available are from Monday to Friday and first and last week might have fewer values.
The only relevant question I found is How to group dates by weeks but it is not suitable for my case.
You can use this method to get the week-number of a given DateTime. Then you can use Enumerable.GroupBy with an anonymous type containing the year and the weeknum:
var yearWeekGroups = allDates.GroupBy(d => new { d.Year, WeekNum = GetIso8601WeekOfYear(d) });
If you want a List<List<DateTime>> where each sub-list contains the dates of a week:
List<List<DateTime>> allWeeks = yearWeekGroups.Select(g => g.ToList()).ToList();
If your country doesn't use ISO 8601 you can use Calendar.GetWeekOfYear:
var cc = CultureInfo.CurrentCulture;
var yearWeekGroups = allDates.GroupBy(d => new
{
d.Year,
WeekNum = currentCulture.Calendar.GetWeekOfYear(d, cc.DateTimeFormat.CalendarWeekRule, cc.DateTimeFormat.FirstDayOfWeek)
});
I currently have a multidimensional array on which I would like to print out all rows given the criteria of a month. So if January is selected, it prints out all the data between 1930 and 2016 as long as the month is January. Any suggestions? As for the year the same principle, with the year selected and printing all values for that year.
It's unclear for me what kind of multidimensional array you want; in any case, I suggest using Linq, e.g to get IEnumerable<DateTime> which you can (easily?) transform into structure you actually want:
int month = 1; // 1 for Jan, 2 for Feb etc.
var dates = Enumerable
.Range(1930, 2016 - 1930 + 1)
.SelectMany(year => Enumerable
.Range(1, DateTime.DaysInMonth(year, month))
.Select(day => new DateTime(year, 1, day)));
// Let's test
String report = String.Join(Environment.NewLine,
dates.Select(date => date.ToString("dd-MM-yyyy")));
// 01-01-1930
// 02-01-1930
...
// 31-01-1930
// 01-01-1931
// ...
// 31-01-2016
Console.Write(report);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Linq orderby, start with specific number, then return to lowest
I need to create a ComboBox that lists the months of the year, but need it to start with the
current month, and then the rest in month order, e.g.:
October
November
December
January
February
March
etc.....
The data source is a list of months in a database, which are numbered according to month number (i.e. January = 1, etc.) and then manipulated to give a datetime
How can I sort this list in C# so that I get the order I want?
TIA.
string[] months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
var ordered = months.Skip(DateTime.Today.Month - 1)
.Concat(months.Take(DateTime.Today.Month - 1))
.Where(s=>!String.IsNullOrEmpty(s))
.ToList();
Use DateTimeFormatInfo.GetMonthName methoed
List<string> list = new List<string>();
DateTimeFormatInfo dtFI = new DateTimeFormatInfo();
DateTime currentDate = DateTime.Now;
DateTime nextyearDate = currentDate.AddYears(1).AddDays(-1);
while (currentDate < nextyearDate)
{
list.Add(dtFI.GetMonthName(currentDate.Month));
currentDate = currentDate.AddMonths(1);
}
This will create a new list of months, starting from current month.
Another take on this with LINQ:
// month name source, use what you prefer
var monthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
var sorted = Enumerable.Range(1, 12).Zip(monthNames, Tuple.Create)
.OrderBy(t => (t.Item1 - DateTime.Today.Month + 12) % 12)
.Select(t => t.Item2)
.ToArray();
You can get the lists of months in C# with List<DateTime> monthNames = DateTimeFormatInfo.CurrentInfo.MonthNames.ToList();. It get a List<DateTime> with 13 elements (a null month name at the end, but you can always remove the last element monthNames.RemoveAt(monthNames.Count - 1);
http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.monthnames.aspx
To reorder this list you can get the current month number index using DateTime.Now.Month.ToString("00");, and restructure the list newMonthNames = monthNames.GetRange(index, 12 - index).AddRange(monthNames.GetRange(0, index);
There are many ways to do it, like others have shown.