Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在列表中查找元素<;T>;并检查是否等于该值_C#_Wpf - Fatal编程技术网

C# 在列表中查找元素<;T>;并检查是否等于该值

C# 在列表中查找元素<;T>;并检查是否等于该值,c#,wpf,C#,Wpf,我在谷歌上找不到如何解决我的问题。 我也看到了微软的文档,但由于某种原因,它不起作用 我用一些属性为我的列表制作了一个类 public class Person { public string Name { get; set; } public string Age { get; set; } public string Count { get; set; } } 然后我在我的主类中创建了我的列表 List<Person> personList = new

我在谷歌上找不到如何解决我的问题。 我也看到了微软的文档,但由于某种原因,它不起作用

我用一些属性为我的列表制作了一个类

public class Person
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string Count { get; set; }
}
然后我在我的主类中创建了我的列表

List<Person> personList = new List<Person>();
不要工作

if(personList.Find(x => x.Name.Contains("Test"))
Person result = personList.Find(x => x.Name == "Test");
if(result.Name == "Test")
不要工作

if(personList.Find(x => x.Name.Contains("Test"))
Person result = personList.Find(x => x.Name == "Test");
if(result.Name == "Test")
不要工作

if(personList.Find(x => x.Name.Contains("Test"))
Person result = personList.Find(x => x.Name == "Test");
if(result.Name == "Test")
我收到的消息就像我无法将Person转换为string/bool。 如果我尝试结果一,我会得到一条消息,该对象未设置为对象实例。 我不理解这个错误,因为我在主类的开头创建了一个实例。 我还认为我需要一个空检查。因为我想在项目在列表中之前检查项目是否存在。这是一件大事。我的想法的完整代码:

TreeViewItem treeViewItem = sender as TreeViewItem;
DockPanel dockpanel = treeViewItem.Header as DockPanel;
List<TextBlock> textblock = dockpanel.Children.OfType<TextBlock>().ToList();
TextBlock name = textblock[0];
TextBlock age = textblock[1];
Person test = personList.Find(x => x.Name == name.Text);
if(test.Name == name.Text)
{
    MessageBox.Show(test.Name);
    test.Count++;
}
personList.Add(new Person { Name = name.Text, Count = 1, Age = age.Text });
CollectionViewSource itemCollectionViewSource;
itemCollectionViewSource = (CollectionViewSource)(FindResource("ItemCollectionViewSource"));
itemCollectionViewSource.Source = personList;
TreeViewItem TreeViewItem=发送方作为TreeViewItem;
DockPanel DockPanel=treevieItem.Header作为DockPanel;
List textblock=dockpanel.Children.OfType().ToList();
TextBlock name=TextBlock[0];
TextBlock age=TextBlock[1];
persontest=personList.Find(x=>x.Name==Name.Text);
if(test.Name==Name.Text)
{
MessageBox.Show(test.Name);
test.Count++;
}
添加(新人物{Name=Name.Text,Count=1,Age=Age.Text});
CollectionViewSource项目CollectionViewSource;
itemCollectionViewSource=(CollectionViewSource)(FindResource(“itemCollectionViewSource”);
itemCollectionViewSource.Source=个人列表;
使用LINQ非常简单:

if(personList.Any(p=> p.Name == "Test"))
{
    // you have to search that person again if you want to access it
}
使用
列表。查找
您必须检查
null

Person p = personList.Find(x => x.Name == "Test");
if(p != null)
{
    // access the person safely
}
但如果需要
人员
,也可以使用LINQ:

Person p = personList.FirstOrDefault(x => x.Name == "Test");
if(p != null)
{
    // access the person safely
}
顺便说一下,还有一个类似于可枚举的
列表
方法。Any
,:


可能是okay的副本。我不知道LINQ是什么,所以我会开始读一些关于它的东西。LINQ比列表搜索更好吗?@Luranis:列表方法很少,但可以使用的LINQ方法更多。另外,虽然list方法很好,但是如果您想从list改为说
HashSet
Person[]
,该怎么办?您必须更改所有代码,并且在另一个集合类中可能没有类似的方法。LINQ可以与任何
IEnumerable
一起工作,所以你不会被困在
列表中。所以,如果我能使用LINQ,那么总是使用它更好吗?@Luranis:不,可能总是有LINQ方法,但这并不意味着LINQ总是更好。它可以帮助降低代码的复杂性(一旦您理解了它),但它并不总是最有效的方法。特别是当您必须查看集合中的上一个或下一个项目时,LINQ不是最佳方法,而是简单的for循环。