6

I am trying to learn how to use lists in C#. There are a lot of tutorials out there, but none of them really explain how to view a list that contains a record.

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 a messagebox. Right now I am just making sure the information is going into the list. I also want to learn how to find a value in the list and get the other information that is related to it, such as, I want to find the item by the Object Number and if it is in the list then it will return the address. I am also using WPF, if that makes a difference. Any help will be appreciated. Thank You.

nawfal
  • 70,104
  • 56
  • 326
  • 368
JLott
  • 1,818
  • 3
  • 35
  • 56
  • 4
    you could use a linq query, check [this](http://stackoverflow.com/a/1175662/647884). – Bastardo Jun 15 '12 at 13:38
  • if ObjectNumber is unique, then you might want to make that a dictionary (instead of list) with the `TKey` being the type of string to hold the object number. – Matthew Jun 15 '12 at 13:44
  • I would have used a dictionary, but the user is going to have to be able to update the Object Number when the "product" is replaced or changed out. – JLott Jun 15 '12 at 13:45

5 Answers5

5

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

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

Here's a possible implementation of ToString:

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 learn how to find a value in the list and get the other information that is related to it, such as, I want to find the item by the Object Number and if it is in the list then it will return the address.

There are several ways to search a 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;
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Amazing how just a few lines can teach a person so much. Thank you for updating your answer, it was very helpful! – JLott Jun 15 '12 at 14:13
2

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

StringBuilder sb = new StringBuilder();

foreach (ObjectProperties op in Properties) 
{
    sb.Append(op.ObjectNumber + "\n");
}

sb.ToString(); // show this in messagebox
saluce
  • 13,035
  • 3
  • 50
  • 67
2

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'));
saluce
  • 13,035
  • 3
  • 50
  • 67
ist_lion
  • 3,149
  • 9
  • 43
  • 73
  • Awesome. I think this is what I am going to try in order to search for the values. Could you possibly explain how I could get just the ObjectAddress Property from this. Thank you for your help. – JLott Jun 15 '12 at 14:02
  • Assuming your ObjectNumber is the Unique ID you could do something like the following: var objAddress = from p in Properties select p.ProductName where p.ObjectNumber.Equals('YourVariable'); – ist_lion Jun 15 '12 at 14:17
  • If you need a good cheat sheet = http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b – ist_lion Jun 15 '12 at 14:20
2

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

I'd recommend taking a look at the MSDN documentation of List<T> and IEnumerable<T>. Go through available methods and see the examples. If you have any specific questions, come 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();
SteveC
  • 15,808
  • 23
  • 102
  • 173
Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
1

Once you have your list you can loop through it using a foreach loop and output the values that way.

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

For example:

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

This would return the first record where the ObjectNumber was 10.

Let me know if you need more clarification.

Gaz Winter
  • 2,924
  • 2
  • 25
  • 47