简体   繁体   English

从List <>获取特定项目

[英]Getting an specific item from List<>

I have a problem! 我有个问题! I have found allots of people with the same problem but none of the answers helpt me.. Im trying to get an specific item from List<> but my result of "test" returns null, why? 我发现有人遇到同样的问题,但没有答案对我有帮助。我试图从List <>获取特定项目,但我的“ test”结果返回null,为什么?

public MainWindow()
{
    InitializeComponent();

    var modelList = new Model2();
    modelList.MyPropertyList.Add(new Model1 { Name = "Hej1", MyProperty1 = true });

    modelList.MyPropertyList.Add(new Model1 { Name = "Hej2", MyProperty1 = false });

    var test = modelList.MyPropertyList.Find(x => x.Name == "Hej1").MyProperty1;
}

According to the OP Comments 根据OP评论

how do you know it is null? 您怎么知道它为空? – dotctor 1 hour ago – dotctor 1小时前
When i debug the value is null.. – Dennis Eriksson 1 hour ago 当我调试时,该值为null。-– Dennis Eriksson 1小时前


are you sure you check the value after executing the line? 确定执行该行后检查值吗? try adding `MessageBox.Show(test.ToString());) and see what is the result – dotctor 1 hour ago 尝试添加`MessageBox.Show(test.ToString());)看看结果是什么– dotctor 1小时前


I feel a shame of my question.. It was working the whole time! 我为我的问题感到羞耻。.它一直在起作用! It was my fault that i read the value before it was declared to "test"! 我在将值声明为“测试”之前读取该值是我的错! But thanks!! 但是谢谢! – Dennis Eriksson 1 hour ago – Dennis Eriksson 1小时前


I think the problem is in the way you debugged your program. 我认为问题出在调试程序的方式上。 You have put a breakpoint on line var test = modelList.MyPropertyList.Find(x => x.Name == "Hej1").MyProperty1; 您已经在var test = modelList.MyPropertyList.Find(x => x.Name == "Hej1").MyProperty1;上放置了一个断点var test = modelList.MyPropertyList.Find(x => x.Name == "Hej1").MyProperty1; and the execution stops right before this line but you think that this line is already executed and Visual Studio shows the value of test as null in Autos window and this makes you think that the test is really null. 并且执行立即在该行之前停止, 但是您认为该行已经执行,并且Visual Studio在Autos窗口中将test的值显示为null ,这使您认为该test实际上为null。 If you continue execution by pressing F10 or add a line like MessageBox.Show(test.ToString()); 如果按F10 继续执行或添加诸如MessageBox.Show(test.ToString()); just to make the previous line executed or somehow show the value of test you will find that it is not null . 只是为了使上一行执行或以某种方式显示test的值,您会发现它不是null

Not much as an answer - but your code - should work fine. 答案不多-但您的代码应该可以正常工作。

void Main()
{
     var modelList = new Model2();
        modelList.MyPropertyList.Add(new Model1 { Name = "Hej1", MyProperty1 = true });
        modelList.MyPropertyList.Add(new Model1 { Name = "Hej2", MyProperty1 = false });
        var test = modelList.MyPropertyList.Find(x => x.Name == "Hej1").MyProperty1;
        Console.WriteLine (test);
}


public class Model1
{
 public string Name { get; set; }
 public bool? MyProperty1 { get; set; }
}

public class Model2
{
 public List<Model1> MyPropertyList { get; set; }
 public Model2()
 {MyPropertyList = new List<Model1>();
 }
}

Result : True . 结果: True

Instead of Find() try using the following: 代替使用Find(),请尝试使用以下命令:

var test = modelList.MyPropertyList.SingleOrDefault(model => model.Name == "Hej1");

if(test != null)
{
    //-- do something here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM