2013-12-18 56 views
0
我有一個用戶定義泛型列表

如何使用使用LINQ聲明

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; 
    } 
} 

而下面的代碼是用於將數據添加到列表

從集合中刪除: -

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

感謝此代碼工作 –