-2

I have an asp.net application where I have 4 List collection objects

List<string> sTransID = new List<string>();
List<string> sOriginID = new List<string>();
List<string> sDestID = new List<string>();
List<string> sCourierID = new List<string>();

These objects are populated at different sections of the application (inside a class, or an aspx code behind etc). The page performance is significantly slow when List elements size increase.

What would be the fastest way to loop through these objects when reading their values (in order to avoid having to loop through 4 objects) ? Can I merge these objects into a parent List object and loop through the parent?

Update:
By merge, I mean something like:

var Parent = List<sTransID>, List<sOriginID>, List<sDestID>, List<sCourierID>  
var GetLocation = Parent[0];  // will return TransID[0], Origin[0], DestID[0], CourierID[0]
Csharp
  • 2,916
  • 16
  • 50
  • 77

2 Answers2

0

I have an Extension method that merges dictionaries. It might be of some help if you modify it for Lists.

public static class DictionaryExtensions
{
    public static T MergeLeft<T, K, V>(this T me, params IDictionary<K, V>[] others)
        where T : IDictionary<K, V>, new()
    {
        var newMap = new T();

        foreach (var p in (new List<IDictionary<K, V>> { me }).Concat(others).SelectMany(src => src))
        {
            newMap[p.Key] = p.Value;
        }

        return newMap;
    }

}

used as follows:

var mergedDictionary = new Dictionary<string, string>().MergeLeft(dic1, dic2, dic3);
DaveDev
  • 41,155
  • 72
  • 223
  • 385
0

One way would be to just concat the lists together before looping:

var itemsToLoop = sTransID.Concat(sOriginID).Concat(sDestID).Concat(sCourierID);
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162