11

If I have a list of some class like this:

class Info {
    public string Name { get; set; }
    public int Count { get; set; }
}

List<Info> newInfo = new List<Info>()
{
    {new Info { Name = "ONE", Count = 1 }},
    {new Info { Name = "TWO", Count = 2 }},
    {new Info { Name = "SIX", Count = 6 }}
};

Can a Lambda expression be used to string join the attributes in the list of classes like this:

"ONE(1), TWO(2), SIX(6)"

gdoron
  • 147,333
  • 58
  • 291
  • 367
Darin Peterson
  • 1,262
  • 2
  • 15
  • 25

3 Answers3

20
string.Join(", ", newInfo.Select(i => string.Format("{0}({1})", i.Name, i.Count)))

You could also override ToString.

class Info
{
   ....
   public override ToString()
   {
        return string.Format("{0}({1})", Name, Count);
   }
}

... and then the call is dead simple (.Net 4.0):

string.Join(", ", newInfo);
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
10
String.Join(", ", newInfo.Select(i=>i.Name+"("+i.Count+")") );
asawyer
  • 17,642
  • 8
  • 59
  • 87
1

You Can use as like following

You can Return a specific type like this

Patient pt =  dc.Patients.Join(dc.PatientDetails, pm => pm.PatientId, pd => pd.PatientId,
                         (pm, pd) => new
                         {
                             pmm = pm,
                             pdd = pd
                         })
                         .Where(i => i.pmm.PatientCode == patientCode && i.pmm.IsActive || i.pdd.Mobile.Contains(patientCode))
                         .Select(s => new Patient
                         {
                             PatientId = s.pmm.PatientId,
                             PatientCode = s.pmm.PatientCode,
                             DateOfBirth = s.pmm.DateOfBirth,
                             IsActive = s.pmm.IsActive,
                             UpdatedOn = s.pmm.UpdatedOn,
                             UpdatedBy = s.pmm.UpdatedBy,
                             CreatedOn = s.pmm.CreatedOn,
                             CreatedBy = s.pmm.CreatedBy
                         })

Or You can retrieve anonymous type like this

var patientDetails = dc.Patients.Join(dc.PatientDetails, pm => pm.PatientId, pd => pd.PatientId,
                 (pm, pd) => new
                 {
                     pmm = pm,
                     pdd = pd
                 })
                 .Where(i => i.pmm.PatientCode == patientCode && i.pmm.IsActive || i.pdd.Mobile.Contains(patientCode))
                 .Select(s => new 
                 {
                     PatientId = s.pmm.PatientId,
                     PatientCode = s.pmm.PatientCode,
                     DateOfBirth = s.pmm.DateOfBirth,
                     IsActive = s.pmm.IsActive,                     
                     PatientMobile = s.pdd.Mobile,
                     s.pdd.Email,
                     s.pdd.District,
                     s.pdd.Age,
                     s.pdd.SittingId

                 })
Md. Nazrul Islam
  • 2,809
  • 26
  • 31
  • This is not the same as a `string.Join()` I think you may have misunderstood the question. There is an already a well-established question about [Join/Where with LINQ and Lambda](https://stackoverflow.com/a/2767742). – user692942 Jul 07 '20 at 16:43