How to view the values ​​stored in a list?

I am trying to learn how to use lists in C #. There are many tutorials, but none of them explain how to view the list containing the entry.

Here is my code:

class ObjectProperties { public string ObjectNumber { get; set; } public string ObjectComments { get; set; } public string ObjectAddress { get; set; } } List<ObjectProperties> Properties = new List<ObjectProperties>(); ObjectProperties record = new ObjectProperties { ObjectNumber = txtObjectNumber.Text, ObjectComments = txtComments.Text, ObjectAddress = addressCombined, }; Properties.Add(record); 

I want to display the values ​​in the message box. Now I just made sure that the information is on the list. I also want to find out how to find the value in the list and get other information related to it, for example, I want to find the element by the number of the object, and if it is in the list, it will return the address, I also use WPF, if that matters. Any help would be appreciated. Thanks.

+6
source share
5 answers

The best way is to override ToString in your class and use string.Join to join all your posts:

 var recordsAsString = string.Join(Environment.NewLine, Properties.Select(p => p.ToString())); MessagBox.Show(recordsAsString); 

An implementation of ToString is possible here:

 class ObjectProperties { public string ObjectNumber { get; set; } public string ObjectComments { get; set; } public string ObjectAddress { get; set; } public override string ToString() { return "ObjectNumber: " + ObjectNumber + " ObjectComments: " + ObjectComments + " ObjectAddress: " + ObjectAddress; } } 

I also want to find out how to find the value in the list and get other information related to it, for example, I want to find the element by the number of the object, and if it is in the list, it will return the address.

There are several ways to search for List<T> , here are two:

 String numberToFind = "1234"; String addressToFind = null; // using List<T>.Find method ObjectProperties obj = Properties.Find(p => p.ObjectNumber == numberToFind); //using Enumerable.FirstOrDefault method (add using System.Linq) obj = Properties.FirstOrDefault(p => p.ObjectNumber == numberToFind); if (obj != null) addressToFind = obj.ObjectAddress; 
+5
source

To display items in a list, you can iterate over the list and get information from it.

 StringBuilder sb = new StringBuilder(); foreach (ObjectProperties op in Properties) { sb.Append(op.ObjectNumber + "\n"); } sb.ToString(); // show this in messagebox 
+2
source

Depends on what you want to do.

If you want to try to find some data, use this code:

 List<ObjectProperties> Properties = new List<ObjectProperties>(); var result = Properties.Where(n => n.ObjectNumber.Equals('yourVariableHere')); 
+2
source

List<T> class implements IEnumerable<T> , which allows you to use a whole bunch of very useful methods for querying a list.

I would recommend taking a look at the MSDN List<T> and IEnumerable<T> documentation. Go through the available methods and see examples. If you have any specific questions, go back to SO.

Here's how you can accomplish what you asked as an example:

 string address = myList .Where(x=>x.ObjectNumber=="A123") .Select(x=>x.ObjectAddress) .First(); 
+2
source

Once you have your list, you can pass it through the foreach loop and output the values ​​this way.

You can also use linq to query your list and return the values ​​you want.

For instance:

  properties.Where(x=>x.ObjectNumber == 10).FirstOrDefault() 

This will return the first record where ObjectNumber will be 10.

Let me know if you need more clarification.

+1
source

Source: https://habr.com/ru/post/918186/


All Articles