Reputation: 5002
I use this query in LINQ to get grouped data:
string fmt = "yyyyMM";
var pivotedMeanResults =
meanDataResults
.GroupBy(i => i.Description)
.Select(g => new PivotedMeanData
{
Description = g.Key,
Month3Value = g.Where(c => DateTime.ParseExact(c.CollectionPeriod, fmt, invariantCulture).Month == currentMonth - 3).FirstOrDefault().LabResultNo.ToString(),
Month2Value = g.Where(c => DateTime.ParseExact(c.CollectionPeriod, fmt, invariantCulture).Month == currentMonth - 2).FirstOrDefault().LabResultNo.ToString(),
Month1Value = g.Where(c => DateTime.ParseExact(c.CollectionPeriod, fmt, invariantCulture).Month == currentMonth - 1).FirstOrDefault().LabResultNo.ToString()
Month3Name = ? //
Month2Name = ? //
Month1Name = ? //
}).ToList();
Data is in this format:
Item Collection_Period Value
==== ================= =====
Item4 201308 19
Item3 201209 2.1
Item2 201307 345
Item1 201309 13.11
Item2 201308 34
Item4 201308 58.2
Item3 201209 2.4
Item2 201309 12.1
Item1 201209 12.3
I need to manipulate data into this format, which, I am getting as needed:
Item Month3Name Month2Name Month1Name
===== ========= ========= =========
Item1 13.11
Item2 345 34 12.1
Item3
Item4 19 58.2
but I want to know if it is possible to get the month names (Month1Name, Month2Name, Month3Name) based on how the equivalent month values are derived in the grouping?
PS: I know I can generate month names can be generated like this:
CultureInfo cInfo = new CultureInfo("en-US");
DateTimeFormatInfo english = cInfo.DateTimeFormat;
english.GetAbbreviatedMonthName(integer)
So I tried this
Month3Name = english.GetAbbreviatedMonthName
(g.Where(DateTime.ParseExact(c.CollectionPeriod, fmt, invariantCulture)
.Month == currentMonth - 3))
but I get the errors:
Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable
<Namespace.Type.MeanData>
' to 'int'The best overloaded method match for 'System.Globalization.DateTimeFormatInfo.GetAbbreviatedMonthName(int)' has some invalid arguments
Upvotes: 0
Views: 1800
Reputation: 2685
Just format dates like below; It will return the month name of the date.
string month = DateTime.Now.ToString("MMMM", new CultureInfo("en-US"));
//or
month = new DateTime(1, 3, 1).ToString("MMMM", new CultureInfo("en-US"));
Upvotes: 0
Reputation: 5161
You want the name of the current month -1, -2, -3.
Then why are you trying tho get the name of the month from a subset of your data??
Month3Name = english.GetAbbreviatedMonthName(currentMonth - 3)
seems a lot more likely to give yo a monthname?
And yes, you cannot subtract 2 from January and expect a valid month :)
Upvotes: 1