简体   繁体   中英

C# How to iterate through table rows and only add certain field names and values to SortedList

I have a table like so...

FACILTY_NAME    FACILITYID  FIELDID MUSYM   MUKEY   SlopeMidPoint   MLRA_ID Shape_Area  MLRASYM FIPS_C
JMB Feeders 3557    18894   1839    1698712 6   22  34578.52926120000   72  08 56 20 31
JMB Feeders 3557    18894   1815    1698708 0   22  315937.13060200000  72  9 56 20 31
JMB Feeders 3557    18894   1650    1698692 0   22  232183.66600100000  72  10 56 20 31
JMB Feeders 3557    18894   5101    1698651 2   22  530405.24224000000  72  11 56 20 31
JMB Feeders 3557    18894   1506    1698655 6   22  94914.03552020000   72  12 56 20 31
JMB Feeders 3557    18894   1816    1698709 2   22  1036.37023796000    72  13 56 20 31
JMB Feeders 3557    18894   5100    1698650 0   22  532206.08141000000  72  14 56 20 31
JMB Feeders 3557    18894   1736    1698707 6   22  7.45379122495   72  15 56 20 31

I only need to grab certain Fields/Values from each row ( Shape_Area , SlopeMidPoint , and MUKEY ). My question is: how can I add each of these field values to a SortedList ? I have looked at the SortedList class and it appears to me that you can only have two elements (Key, Value). How can I store three elements? Or, should I be looking at another class like a List or ArrayList ?

Do you need to use a SortedList ? That will give you a key/value pair as you mentioned. You could just generate a class that maps to your data structure.

class Facility
{
    public string Name { get; set; }
    public int Id { get; set; }
    public int FieldId { get; set; }
    //etc, etc
}

Then, when reading your data, whether it is from a database or a flat file, you can simply create a Facility object for each row in your data set and add them to a regular List<Facility> .

List<Facility> facilities = new List<Facility>();

Facility facility = new Facility() { Name = "Foo", Id = 1 /* etc */ };
facilities.Add(facility);

You can then use LINQ to get values:

var results = yourList.Where(s => s.Id == 3557);

Or any other property on your data structure.

class SomeClass
{
  string shape_Area {get; set;} 
  string SlopeMidPoint {get; set;}
  string MUKEY  {get; set;}
}

SortedList.Add method takes key and value as object, so now you can add SomeClass As the value

like this:

sortedList.Add(someClass.MUKEY, someClass);

This way you will get a list sorted by MUKEY

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM