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.

4

5 回答 5

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;
于 2012-06-15T13:43:22.450 回答
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
于 2012-06-15T13:42:24.427 回答
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'));
于 2012-06-15T13:42:33.033 回答
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();
于 2012-06-15T13:43:26.290 回答
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.

于 2012-06-15T13:40:58.993 回答