Monday, April 21, 2014

LINQ to display list of values separated by a comma



Requirement: We need to display data as a single row of values separated by commas.

Solution:
   Object obj = new Object();

var dbquery = (from g in db.table1
               join gc in db.table2
               on g.id equals gc.id
               where g.prop1 == value1 && gc.prop2 == value2
               select g);

          if (dbquery!= null && dbquery.Count() > 0)
            {
         // Gets all the groups/tags the lead belongs to and joins them with a ','
                obj.PropName=string.Join(",",dbquery.Select(x=>                                                x.propName)); 
           

            }

No comments:

Post a Comment