2013-12-18 4 views
0
내가 한 사용자가
public class DoctorsData 
{ 
    string _doctorName; 
    public string DoctorName { get { return _doctorName; } } 

    string _doctorAge; 
    public string DoctorAge { get { return _doctorAge; } } 

    string _doctorCity; 
    public string DoctorCity 
    { 
     get { return _doctorCity; } 
     set { _doctorCity = value; } 
    } 

    string _doctorDesc; 
    public string desc 
    { 
     get 
     { 
      return _doctorDesc; 
     } 
     set 
     { 
      _doctorDesc = value; 
     } 
    } 

    public DoctorsData(string doctorname, string doctorage, string doctorcity, string doctordesc) 
    { 
     _doctorName = doctorname; 
     _doctorAge = doctorage; 
     _doctorCity = doctorcity; 
     _doctorDesc = doctordesc; 
    } 
} 

그리고 코드 아래사용 LINQ 문

목록에 데이터를 추가하는 것입니다 일반적인 목록을 정의 데

를 사용하여 콜렉션에서 삭제하는 방법 : -

List<DoctorsData> doctorlist = new List<DoctorsData>(); 
doctorlist.Add(new DoctorsData("mukesh", "32","sirsa","aclass")); 
doctorlist.Add(new DoctorsData("rajesh", "29","hisar","bclass")); 
doctorlist.Add(new DoctorsData("suresh", "25","bangalore","cclass")); 
doctorlist.Add(new DoctorsData("vijay", "24","bangalore","cclass")); 
doctorlist.Add(new DoctorsData("kumar anna", "40","trichi","aclass")); 

내 요구 사항은 난입니다 30 세 미만의 모든 의사 항목을 삭제하고 싶습니다. 어떻게 LINQ를 사용하여이 작업을 수행 할 수 있습니까?

답변

2

이 시도 :

doctorList.RemoveAll(doctor => int.Parse(doctor.DoctorAge) < 30); 

당신은 DoctorAge 정수

+0

감사합니다.이 코드가 작동합니다. –

1
int age = 0 
doctorList.RemoveAll(D => int.TryParse(D.DoctorAge,out age) && age < 30); 

이 도움이 될 것입니다 희망으로 구문 분석 할 수 있는지 확인하기 위해 몇 가지 추가 검사를 추가 할 수 있습니다.

+0

감사합니다.이 코드가 작동합니다. –