1

I'm a Electronic Engineer.

I have a strugle in a program I made for an Windows Form Application in C# and I need your help to deal with it. I have two Lists of strings [num] and [work] with the same size and each value of [num] corresponds to [work] like this:

num work
11  0,4
23  0,5
36  0,333
49  13
...

Then I have the List of strings [lnum] wich is almost like [num] but with less numbers like this:

lnum
23
36
49
...

And What I want to do is to get the correspond value to the numbers that exists in the lnum like this:

lnum lwork
23   0,5    
36   0,333
49   13
...  

My code to do so that I've developed is this:

List<string> num = new List<string>();
List<string> work = new List<string>();

List<string> lnum = new List<string>();
List<double> lwork = new List<double>();

for (int i = 0; i < num.Count; i++)
{
    for (int j = 0; j < lnum.Count; j++)
    {
        if(lnum[j] == num[i])
        {
            lwork.Add(Convert.ToDouble(work[j].ToString()));
        }
    }
}

Now the problem is that It seems I'm not doing it in the rigth way if the Lists of numbers have a lot of variables. What I'm doing wrong? What is the best way to do this? What changes of my code I need to make?

If you need further more explanation I'll try to explain with more detail. Sorry for any mistake in English, I'm Portuguese and English is not my nature language and I apolagyse if the title doesn't describe better my struggle.

I really really need your help people, and I'll be forever grateful for your help. I hope the best weekend for all of you!

user3664117
  • 49
  • 2
  • 11
  • 3
    If both lists are linked via index use a `Dictionary` instead(if no duplicate `Num` are possible) or create a class with two properties and fill a `List` instead. – Tim Schmelter Apr 23 '15 at 10:09
  • This looks like a "SortedDictionary" would do what you want (or "map" in other languages) – Skizz Apr 23 '15 at 10:10
  • you want intersected list ? if yes then take a look http://stackoverflow.com/questions/7187996/intersect-two-list-in-c-sharp – Kaushik Apr 23 '15 at 10:12
  • seems you need [List.Intersect Method](https://msdn.microsoft.com/en/library/vstudio/bb910215(v=vs.90).aspx) – Grundy Apr 23 '15 at 10:13
  • I don't think this code snippet does what the OP is asking for. Perhaps if the requirements were set out without code we could suggest ways to implement it? – Skizz Apr 23 '15 at 10:15
  • The List lnum is already a intersect value from diferent Lists like num. So lnum is already a shorter than num. – user3664117 Apr 23 '15 at 10:17
  • The num values corresponds to work values because the [num] have the ID number of an employee and [work] have the number of extra hours of work. In [lnum] I need the same values of [work] in the List [lwork] to operate some calculations in my program. Hope that I've explaned well and I'm thankfull for your answers people. – user3664117 Apr 23 '15 at 10:23

2 Answers2

2

Build a dictionary from you num and work lists:

Dictionary<string,string> lookup = new Dictionary<string,string>();
for(int i = 0; i < num.Count; i++)
    lookup.Add(num[i], work[i]);

With this lookup dictionary it is easy to create the new pairings:

for(int i = 0; i < lnum.Count; i++)
{
    string lkey = lnum[i];
    string lwork = lookup[lkey];
    ....
}

Of course you should add some error handling, for example if lnum contains strings not found in lookup

DrKoch
  • 9,556
  • 2
  • 34
  • 43
1

You can select the lwork values using linq:

Dictionary<String, String> origin = new Dictionary<String, String>();
            // TODO populate origin with key = num and value = work
            PopulateOrigin(origin);

            List<String> lnum = new List<String>();
            // TODO populate lnum list
            PopulateLNum(lnum);

            List<Double> lwork = origin.Where(kv => lnum.Contains(kv.Key)).Select(kv => Convert.ToDouble(kv.Value)).ToList<Double>();
Massimo Zerbini
  • 3,125
  • 22
  • 22