mukesh insa
mukesh insa

Reputation: 23

how to use the delete from collection using LINQ Statement

I am having one user define generic list

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

And below code is for adding data to list :-

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

My requirement is i want to delete the all the doctors entry having age less than 30.How we can perform this operation using LINQ .

Upvotes: 0

Views: 91

Answers (2)

Ahmy
Ahmy

Reputation: 5480

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

Hope this will help.

Upvotes: 1

R&#233;da Mattar
R&#233;da Mattar

Reputation: 4381

Try this :

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

You can add some extra checks to be sure that DoctorAge can be parsed as an integer

Upvotes: 2

Related Questions