【问题标题】:how to use the delete from collection using LINQ Statement如何使用 LINQ 语句从集合中删除
【发布时间】:2013-12-18 09:50:56
【问题描述】:

我让一个用户定义通用列表

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 执行此操作。

【问题讨论】:

    标签: c# .net linq c#-4.0 linq-to-entities


    【解决方案1】:

    试试这个:

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

    您可以添加一些额外的检查以确保 DoctorAge 可以被解析为整数

    【讨论】:

      【解决方案2】:
      int age = 0
      doctorList.RemoveAll(D => int.TryParse(D.DoctorAge,out age) && age < 30);
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2014-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-13
        • 2011-02-11
        相关资源
        最近更新 更多