.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Code Snippets >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Concatenation in LINQ

Posted By:Akhil Raj       Posted Date: January 16, 2014    Points: 25    Category: LINQ    URL: http://www.dotnetspark.com  

If you have one object with properties. The object contains another object means the first object contains multiple entries of second object. But when i show the data, i need to concatenate the second object as comma separated value

In the classes the p3 property of Class1 needs to be concatenate to show the value.  Let us assume i have List<Class1> named class1List.
public class Class1
 public string p1 {get; set; }

 public int p2 {get; set;}
 public List<Class2> p3 { get; set;}

public class Class2
  public string p1 {get; set; }

from A in class1List select new { A.p1, A.p2, p3 = (A.p3!= null && A.p3.Count > 0 ? String.Join(", ", from item in A.p3 select item.p1) : string.Empty) };


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend