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)"

3 Answers 3

21
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);
Sign up to request clarification or add additional context in comments.

2 Comments

If it where my project, I'd go with something like this.
+1, definitely recommend overriding ToString() in this case.
10
String.Join(", ", newInfo.Select(i=>i.Name+"("+i.Count+")") );

Comments

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

                 })

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.