Home > Software design >  Comma separated Names with multiple columns as group by using C#
Comma separated Names with multiple columns as group by using C#

Time:04-08

I have a list which has following result set .

 public class PrExport
    {
        public string pId { get; set; }
        public string prName { get; set; }
        public string hId { get; set; }
        public string hName { get; set; }
        public string np { get; set; }
        public string ftId { get; set; }
        public string paName { get; set; }
    }

List<PrExport> prTable = new List<PrExport>();
prTable =some 3rdparty service returns the result set as below;

enter image description here

I want the result set set as below enter image description here

This is the code I have used.

var res = from c in prTable 
group c by new { c.pId, c.prName, c.hId, c.hName, c.np, c.ftId } into gr
                      select new PrExport()
                      {
                          pId = gr.Key?.pId,
                          prName = gr.Key?.prName,
                          hId = gr.Key?.hId,
                          hName = gr.Key?.hName,
                          np = gr.Key?.np,
                          ftId = gr.Key?.ftId,
                          paName = string.Join(",", gr.Select(c => c?.paName))
                      };
            return prTable = res.ToList();

I'm getting the result set as below .Here I'm not getting comma seperated paName which I wanted.What am I missing here?

enter image description here

CodePudding user response:

As others have pointed out your code seems to be fine.

I don’t know how you produced the pictures of the tables but if that is a CSV file opened in e.g. Excel, it will not allow a comma , inside one of the values unless you use semi colon ; as delimiter in your CSV file.

CodePudding user response:

check this out it may help you.

https://stackoverflow.com/questions/48723477/linq-one-to-many-relations-as-comma-separated-values
  • Related