简体   繁体   中英

How to match items of two list in C#?

I want to get list_ID value base on List_Position. How should I do? Thanks

List<int> list_ID = new List<int> (new int[ ] { 1, 2, 3, 4, 5 });

List<string> list_Position = new List<string> (new string[ ] { A, C, D, B, E});

A = 1,

B = 4,

C = 2,

D = 3,

E = 5,

The best option for you at this moment is Dictionary Class with list_Position as key and list_Position as value So that you can access values based on position and wise Versa. the definition will be like the following:

 Dictionary<int, string> customDictionary= 
            new Dictionary<int, string>();

 customDictionary.Add(1,"A");
 customDictionary.Add(2,"C");
....

If you want to access value corresponds o 2 means you can use

string valueAt2 = customDictionary[2]; // will be "C"

If you want to get the key/s corresponds to the specific value means you can use like the following:

var resultItem = customDictionary.FirstOrDefault(x=>x.value=="C");
if(resultItem !=null) // FirstOrDefault will returns default value if no match found
{
   int resultID = resultItem.Key; 
}

If you still want to go with two lists means you can consider this Example Which means, Get position of the required element from the list_Position and get the element at this position in the list_ID list, Keep in mind list_ID must be greater or equal in number of elements as that of in the list_Position. The code will be like this:

string searchKey="D";
int reqPosition=list_Position.IndexOf(searchKey);
if(reqPosition!=-1)
{
    Console.WriteLine("Corresponding Id is {0}",list_ID[reqPosition]);
}
else
   Console.WriteLine("Not Found");

You can zip the two lists, then do a linq query on the zipped list:

int? id = list_Position.Zip(list_ID, (x, y) => new { pos = x, id = y })
                       .Where(x => x.pos == "B")
                       .Select(x => x.id)
                       .FirstOrDefault();

The above code returns id = 4 .

Like this:

var letterIndex = list_Position.indexOf(B);
var listId = (letterIndex + 1 > list_Id.Count) ? -1 : list_Id[letterIndex];

//listId==4

instead of using two separate lists, one for the value and one for the position, opt for a dictionary, it will make your life easier since it can encapsulate a value and a key.

Dictionary<int, string> dictionary = new Dictionary<int, string>();

dictionary.Add(1, "A");
dictionary.Add(2, "B");
dictionary.Add(3, "C");
dictionary.Add(4, "D");
dictionary.Add(5, "E");

Some of the operations you can do on your dictionary can be, for example :

check if a key exists or not in the dictionary :

if (dictionary.ContainsKey(1))

check if a value exists or not in the dictionary :

if (dictionary.ContainsValue("E"))

access the value that has a certain key :

string value = dictionary[1];

Loop over pairs with foreach :

foreach (KeyValuePair<string, int> pair in dictionary )
{
    Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
}

Use var keyword to enumerate dictionary

foreach (var pair in dictionary)
{
    Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
}

Store keys in a List and Loop through list.

List<string> list = new List<string>(dictionary.Keys);
foreach (string something in list)
{
    Console.WriteLine("{0}, {1}", something, dictionary[something]);
}

remove value from dictionary

dictionary.Remove("A");

You can use Dictionary<int, string> instead of List<int> and List<string> like this:

Dictionary<int, string> yourDic = new Dictionary<int, string>();
yourDic.Add(1, "A");
// ... and so on

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