-6

I have two Columns in Below format in my application (c#)

Month Name   Amount
Jan15   ==2000
Feb15 ==457
Mar15 =200
April15 =4666
May15 = 357
Jan16 = 332
feb16 =323
Mar16 =233

these columns are dynamic (any number of columns can cm in Same format) I need to get the sum of all the amounts where month is after Mar15. How to achieve that. Pseudo code

If MonthName >Mar15
amount = sum (amount)
Shalini Garg
  • 127
  • 1
  • 15
  • Why Jan15 is under month ?? Shouldn't it be Jan or January – Kavindu Dodanduwa Mar 04 '15 at 06:04
  • When do you say that a given month is greater than `Mar15` ? Put that logic in a method and call it to check. – CinCout Mar 04 '15 at 06:06
  • possible duplicate of [Get Month name from month number](http://stackoverflow.com/questions/3184121/get-month-name-from-month-number) – AFract Mar 04 '15 at 06:07
  • Your data is way too inconsistent. Can't even parse the month as 3 letters because of April. – Pierre-Luc Pineault Mar 04 '15 at 06:09
  • See the duplicate topic I suggest, split at fourth character and you'll be done. if you're data are ikconsistent (april) you'll have to do it manually and we won't write it for you so show your tries. – AFract Mar 04 '15 at 06:09
  • @KcDoD in My requirement it includes Year also with month – Shalini Garg Mar 04 '15 at 06:10
  • @AFract Data will be always in below format... Jan-14 Feb-14 Mar-14 Apr-14 May-14 Jun-14 Jul-14 Aug-14 Sep-14 Oct-14 Nov-14 Dec-14 Jan-15 Feb-15 Mar-15 Apr-15 May-15 Jun-15 how to compare Mar14 lesser value in this form of data – Shalini Garg Mar 04 '15 at 06:12
  • Then your sample is wrong. Read my answer again and try something. – AFract Mar 04 '15 at 06:13
  • @AFract duplicate link .,u hv provided convert from datetime format to MMM format ,,i need to convert from MMM format to datetime format to compare – Shalini Garg Mar 04 '15 at 06:18
  • Don't you think it's a little bit related ? – AFract Mar 04 '15 at 06:20

1 Answers1

1

If your DateFormat is always be going to like Jan-14 Feb-14 Mar-14 Apr-14 May-14 Jun-14 then you can try to use the ParseExact to convert it to the DateTime and then apply the SUM on it to get the result you want.

var result = dataList.Where(x => DateTime.ParseExact(x.monthName, "MMM-yy", CultureInfo.InvariantCulture) > DateTime.ParseExact("Mar-15", "MMM-yy", CultureInfo.InvariantCulture))
                     .Sum(x => x.Amount);

In above case it will give the sum of all amounts which has date greater than Mar-15.


UPDATE

To add the Group BY you can simply add as,

.GroupBy(x=>x.Id).Select(x=> new { Person = x.Key,TotalAmount = x.Sum(x=>x.Amount)})
Mahesh
  • 8,694
  • 2
  • 32
  • 53
  • Hey, If i need to add groupby column here then hw cn i add tat – Shalini Garg Mar 04 '15 at 09:48
  • How do you want to group By on which column ? – Mahesh Mar 04 '15 at 09:56
  • Actually in another method i hv one more column Id. Each Id is having these List of Month and amount value. so i need sum of amount where month after Mar-15 for each ID. Consider a scenario like a person spend some amount each month and like tat 50 person are there and i need to sum up for each person expenditure after March 15 – Shalini Garg Mar 04 '15 at 10:02
  • Hi thanks for the help.. one more question,,if i need sum year wise..lik in the sample i need sum for 2014 separately and for 2015 separatly – Shalini Garg Mar 04 '15 at 10:08
  • Hi In the another method i need sum for complete one year. in the above code its returning from Mar-15 to Dec 15 .. if i need sum from Mar-15 till Feb 16 and then Mar16 to Feb 17 n so on.. how to do that – Shalini Garg Mar 11 '15 at 06:06
  • Please ask it as separate question with test data and your tried code. – Mahesh Mar 11 '15 at 06:16