如果我有一个这样的类的列表:
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 }}
};
可以使用Lambda表达式对类列表中的属性进行字符串连接,如下所示:
"ONE(1), TWO(2), SIX(6)"
答案 0 :(得分:16)
string.Join(", ", newInfo.Select(i => string.Format("{0}({1})", i.Name, i.Count)))
你也可以覆盖ToString。
class Info
{
....
public override ToString()
{
return string.Format("{0}({1})", Name, Count);
}
}
...然后调用很简单(.Net 4.0):
string.Join(", ", newInfo);
答案 1 :(得分:9)
String.Join(", ", newInfo.Select(i=>i.Name+"("+i.Count+")") );
答案 2 :(得分:2)
您可以像以下一样使用
您可以返回这样的特定类型
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
})
或者你可以像这样检索匿名类型
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
})