0

I couldn't find on Google how to solve my problem. I also saw Microsoft Documentation but for some reason it wont work.

I've made a Class for my List with some Propertys

public class Person
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string Count { get; set; }
}

Then I created my List in my Main class.

List<Person> personList = new List<Person>();

Now we come to my problem. I want to check if a item with Name property = "Test" exists. If yes i want to show a MessageBox which returns the result. I tried

if(personList.Find(x => x.Name == "Test"))

Don't work.

if(personList.Find(x => x.Name.Contains("Test"))

Don't work.

Person result = personList.Find(x => x.Name == "Test");
if(result.Name == "Test")

Don't work.

I get messages like i can't convert Person into string/bool. if i try the result one i get the message, that the object was not set to an object instance. I don't understand this Error because i created a instance at the beginning of my Main Class. Also I think that I need a null check. Because I want to check if an Item exists before items are in the list. It's a Event. Full Code of my idea:

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;
Luranis
  • 151
  • 1
  • 3
  • 10
  • 1
    Possible duplicate of [Find an item in List by LINQ?](https://stackoverflow.com/questions/1175645/find-an-item-in-list-by-linq) – mjwills Sep 14 '17 at 09:55

1 Answers1

8

Very easy with LINQ:

if(personList.Any(p=> p.Name == "Test"))
{
    // you have to search that person again if you want to access it
}

with List<T>.Find you have to check for null:

Person p = personList.Find(x => x.Name == "Test");
if(p != null)
{
    // access the person safely
}

But you can also use LINQ if you need the Person:

Person p = personList.FirstOrDefault(x => x.Name == "Test");
if(p != null)
{
    // access the person safely
}

By the way, there is also a List<T> method that works like Enumerable.Any, List<T>.Exists:

if(personList.Exists(p=> p.Name == "Test"))
{
    // you have to search that person again if you want to access it
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • okay. I dont know what LINQ is so i will start reading some stuff about it. Is LINQ "better" then searching with List ? – Luranis Sep 14 '17 at 10:00
  • 2
    @Luranis: there are only few list methods but much more LINQ methods that you can use. Also, while the list method is fine, what if you want to change from list to say `HashSet` or `Person[]`? You have to change all your code and maybe there is not a similar method in the other collection class. LINQ works with any `IEnumerable` so you are not stuck to `List`. – Tim Schmelter Sep 14 '17 at 10:03
  • So always if i CAN use LINQ its better to use it? – Luranis Sep 14 '17 at 10:15
  • 1
    @Luranis: no, it's likely that there is always a LINQ approach, but it doesn't mean that LINQ is always better. It _can_ help to reduce complexity of your code(once you have understood it), but it is not always the most efficient way. Especially if you have to look at previous or next items in a collection LINQ is not the best approch but a plain for-loop. – Tim Schmelter Sep 14 '17 at 10:18