0

I want my data in javascript such that it creates arrays for each fund and within those arrays there should be all the date and amount fields like

FundName1
dated amount dated amount dated amount

FundName2 dated amount dated amount dated amount

Or can I throw all the data to javascript and then doing this with with javascript? if Yes then How?

I have data as shown in image

Blue
  • 22,608
  • 7
  • 62
  • 92
MaazKhan47
  • 811
  • 1
  • 11
  • 22
  • Can you please clarify your question? It's quite hard to understand. – Blue Aug 04 '16 at 05:23
  • 1
    You should look up with `GroupBy` documentation. What you're asking is a fairly basic group by. – Enigmativity Aug 04 '16 at 05:28
  • I have this data at CSharp level I want to group By it with FundName or Fund_ID such that FundName appears only once and all dated and amount fields related to that FundName appears with in it – MaazKhan47 Aug 04 '16 at 05:30

1 Answers1

1

You can do that in C#. Since every Fund_ID has a unique Fund_Name, it is safe to group by both columns:

var grouped = (from f in data
              group f by new { F.Fund_ID, F.Fund_Name } into grouping
              select new Class1()
              {
                  Fund_ID = grouping.Key.Fund_ID,
                  Fund_Name = grouping.Key.Fund_Name,
                  Details = grouping.Select(x=> new Class2()
                                     { Amount = x.Amount, Date = x.Dated }).ToList()
              }).ToList();

Assumes you have classes:

public class Class1
{
    public string Fund_ID { get; set; }
    public string Fund_Name { get; set; }
    public List<Class2> Details { get; set; }
}

public class Class2
{
    public string Date { get; set; }
    public string Amount { get; set; }
}
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • Is it possible with eliminating 'Details' ? – MaazKhan47 Aug 04 '16 at 05:43
  • @MaazKhan47 don't you want the amount and date for each `Fund_ID` ? – Zein Makki Aug 04 '16 at 05:45
  • Okay Got it. So in model Details should be list? a list of which data type? – MaazKhan47 Aug 04 '16 at 05:55
  • @MaazKhan47 it is *anonymous type* created on the spot here (google that to read more about it). You can create a class, but since you're passing this to JavaScript, who cares about creating a class :) – Zein Makki Aug 04 '16 at 05:56
  • If you could please help me with anonymous type. How to declare ir in model and then how to use it in Controller? – MaazKhan47 Aug 04 '16 at 06:43
  • @MaazKhan47 Oh you're passing that in MVC, create a class then and use: `Select(x=> new MyClassName() { Amount = x.A ....` – Zein Makki Aug 04 '16 at 06:44
  • This is LINQ Part: var graphData = (from dt in dataset.Tables[0].AsEnumerable() group dt by new { F = dt["FundName"] } into grp select new vm_FundGraph { fundName = grp.Key.F.ToString(), Details = grp.Select(x => new { Amount = x["amount"], date = x["dated"] }).ToList() }).ToList(); – MaazKhan47 Aug 04 '16 at 07:06
  • This is model public class vm_FundGraph { public string date { get; set; } //public string fundID { get; set; } public string Amount { get; set; } public string fundName { get; set; } } – MaazKhan47 Aug 04 '16 at 07:07
  • @MaazKhan47 I updated the answer. – Zein Makki Aug 04 '16 at 07:11
  • Thanks alot @user3185569. I got it clearly :D :) – MaazKhan47 Aug 04 '16 at 08:24