Jon*_*Jon 2 .net c# linq .net-4.0 c#-4.0
如果某个属性的某个条件例如/ empty,我怎样才能使类中的属性返回某个字符串
public class Person
{
public string Name{get;set;}
publc string MiddleName{get;set;}
public string Surname{get;set;}
public string Gender{get;set;}
}
List<Person> people = repo.GetPeople();
List<Person> formatted = people.GroupBy(x=>x.Gender).//?? format Gender to be a certain string eg/"Not Defined" if blank
Run Code Online (Sandbox Code Playgroud)
people.GroupBy(x=>x.Gender ?? "Not Available").ToList();
Run Code Online (Sandbox Code Playgroud)
更新:(捕捉空字符串)
people.GroupBy(x=> String.IsNullOrWhiteSpace(x.Gender) ? "None" : x.Gender).ToList();
Run Code Online (Sandbox Code Playgroud)