Group by single column in Linq c# -


i have table below: need display without duplicates. need groupby customer alone. c1 has both 'name'

id  name    customer 1   xxxx     c1 2   yyyy     c1 

i need values on c1 : xxx ,yyy. getting c1: xxx , c1: yyy.
code is:

public list<data> getcomponentstatus() {     list<data> d= null;     using(var entity=new fm())     {          d = entity.getdata()              .select(                 => new data                 {                     customer = a.id,                     name = a.name,                 })             .groupby(a=>a.customer).select(a=>a.firstordefault()).tolist();     }     return d; } 

from this, getting first record or last record when using lastordefault().

i want both 'name' on single customer c1.

if understood correctly want group customer , name comma seperated. created sample data of yours , wrote linq in query syntax. see demo output on rextester below.

var data = new[] { new {id = 1,name = "xxxx", customer= "c1"},                            new {id = 2,name = "yyyy", customer= "c1"},                          };          var query = in data                     group a.customer grp                     select new {cutomer= grp.key ,name = string.join(",",grp.select (g => g.name)) };          foreach (var e in query)         {           console.writeline("customer: {0} ,name: {1}", e.cutomer,e.name);         } 

its demo on rextester

so code should

public list<data> getcomponentstatus()     {          using(var entity=new fm())         {             var  d =  in entity.getdata()                       group a.customer grp                       select new data {cutomer= grp.key ,name = string.join(",",grp.select (g => g.name))};                  return d.tolist();          }      } 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -