C# 如何从列表中读取变量

本文关键字:读取 变量 列表 | 更新日期: 2023-09-27 18:33:24

我有一个示例列表。

            List<Reminders> remind2 = new List<Reminders>() {
               new Reminders() { Checked = true, Name_re = "Impreza" },
               new Reminders() { Checked = true, Name_re = "Impreza1" },
               new Reminders() { Checked = false, Name_re = "Impreza2" },
               new Reminders() { Checked = true, Name_re = "Impreza3" },
               new Reminders() { Checked = true, Name_re = "Impreza4" },
               new Reminders() { Checked = false, Name_re = "Impreza5" },
               new Reminders() { Checked = true, Name_re = "Impreza6" },
               new Reminders() { Checked = false, Name_re = "Impreza7" }
        };

而且我只有一个变量,例如"Impreza1"。

如何读取此变量的已检查 = true?

C# 如何从列表中读取变量

如果你只想要一个布尔结果,请使用 linq .Any(),即

var text = "Impreza1";
bool isChecked = remind2.Any(x=>x.Name_re==text && x.Checked);

如果你想抓住变量,如果它是真的,试试这个:

var reminder = remind2.FirstOrDefault(x=>x.Name_re==text && x.Checked);
if(reminder!=null)
{
   //its checked..do stuff
}
我认为

LINQ 使这变得非常简单:

    List<Reminders> remind2 = new List<Reminders>() {
           new Reminders() { Checked = true, Name_re = "Impreza" },
           new Reminders() { Checked = true, Name_re = "Impreza1" },
           new Reminders() { Checked = false, Name_re = "Impreza2" },
           new Reminders() { Checked = true, Name_re = "Impreza3" },
           new Reminders() { Checked = true, Name_re = "Impreza4" },
           new Reminders() { Checked = false, Name_re = "Impreza5" },
           new Reminders() { Checked = true, Name_re = "Impreza6" },
           new Reminders() { Checked = false, Name_re = "Impreza7" }
    };
    var name = "Impresa";
    var checkedWithName = from rem in remind2 where rem.Name_re == name && rem.Checked select rem/*.ToList()*/;

如果您需要单个Reminders,则可以对结果调用Single()(http://msdn.microsoft.com/en-us/library/bb155325(v=vs.110).aspx)。

var result = remind2.FirstorDefault(p=> p.Name_re.ToLower().Equals("Impreza1".ToLower());

bool checked = result.Checked;

如果您不确定该项目是否存在,但不应超过一个:

var reminder = remind2.SingleOrDefault(r => r.Name_re == "Impreza1");
if (reminder != null)
// Use the value of reminder.Checked

如果您不确定该项目是否存在,并且可能有多个:

var reminder = remind2.FirstOrDefault(r => r.Name_re == "Impreza1");
if (reminder != null)
// Use the value of reminder.Checked

如果您绝对确定只有一个项目具有给定名称,那么:

var isChecked = remind2.Single(r => r.Name_re == "Impreza1").Checked;

但是,如果每个名称只有一个项目,您将使用错误的数据结构 - 将数据存储在Dictionary<string, bool>中会更适合。

另一种口味(虽然这区分大小写... - 欲了解更多信息,您可以检查例如:http://msdn.microsoft.com/en-us/library/x0b5b5bc%28v=vs.110%29.aspx

//*** Find all occurences Given Checked value - If not found _results2.Count==0
//*** Here x.Checked.Equals is used for boolean value
List<Reminders> _results2 = new List<Reminders>();
_results2 = remind2.FindAll(x => x.Checked.Equals(true));
string _stringToFind="Impreza";
//*** Find 1 occurence Given Name_re - If not found: _result==null
Reminders _result = remind2.Find(x => x.Name_re.Contains(_stringToFind));
//*** Find all occurences Given Nmae_re - If not found _results.Count==0
List<Reminders> _results = new List<Reminders>();
_results = remind2.FindAll(x => x.Name_re.Contains(_stringToFind));