-1

After searching i couldn't link any answer found on this site to my issue i have the following class

I have the following classes

public class Site
{
    public string  Name { get; set; }
    public string Stauts { get; set; }
    public string Demo { get; set; }
    public List<Data> Datas { get; set; }
}
public class Data
{
    public string IPT { get; set; }
    public string Currency { get; set; }
    
    public double Amount { get; set; }
}

I got data from external service in this format

    "Name": "TcR",
"Stauts": "ACT",
"Demo": "BYD",
"IPT": "CATS",
"Currency": "EUR",
"Amount": "58.01",

"Name": "TcR",
"Stauts": "ACT",
"Demo": "BYD",
"IPT": "ROS",
"Currency": "USD",
"Amount": "25.01",

"Name": "TcR",
"Stauts": "ACT",
"Demo": "BYD",
"IPT": "SAP",
"Currency": "EUR",
"Amount": "44.01",

How can i transform this data to have one site and all related Data object in a list?

what i did

var result = from d in Loc
                         group d by new
                         {
                             Name = d.Name,
                             Stauts = d.Stauts,
                             Demo = d.Demo,
                            
                         }
                                  into g
                         select new Site
                         {
                             Name = g.Key.Name,
                             Stauts = g.Key.Stauts,
                             Demo = g.Key.Demo,
                             Datas = g.ToList()/// ==> error here
 
                         };
Maro
  • 2,579
  • 9
  • 42
  • 79
  • I guess you are looking for `G.Value.Select(y=>new Data{}).ToList()`. But it's unclear may we have something that will look like an [mre] – Drag and Drop Feb 05 '21 at 12:39
  • @MahyarMottaghiZadeh, that's a miss conception. LinQ is not SQL and never was. And never will. It's a query language It works on in memory collection, sql, web services, xml document, json, and many other things. It's not bound to data base. – Drag and Drop Feb 05 '21 at 12:43
  • Does this answer your question? [Group by in LINQ](https://stackoverflow.com/questions/7325278/group-by-in-linq) – Self Feb 05 '21 at 12:43

1 Answers1

1

Assuimng you have that values in a list named _list, here's what you could do:

var grouped = _list
    .GroupBy(item => new { item.Name, item.Status, item.Demo })
    .Select(g => new Site()
        {
            Name = g.Key.Name,
            Status = g.Key.Status,
            Demo = g.Key.Demo,
            Datas = g.Select(groupItem => new Data() 
                {
                    IPT = groupItem.IPT,
                    Currency = groupItem.Currency,
                    Amount = groupItem.Amount,
                }).ToList(),
        })
    .ToArray();
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69