0

Assuming the 2 string arrays are the same length and not empty how can I make a List of the contents?

I had a Dictionary working, but now I need to be able to use duplicate keys so I am resorting to a List.

string[] files = svd.file.Split(",".ToCharArray());
string[] references = svd.references.Split(",".ToCharArray());

Dictionary<string, string> frDictionary = new Dictionary<string, string>();

frDictionary = files.Zip(rReferences, (s, i) => new { s, i })
.ToDictionary(item => item.s, item => item.i);

I could do it like:

List<string, string> jcList = new List<string, string>();

and then just have a double loop into the two arrays but I know a faster way must exist.

Pete
  • 3
  • 1
  • `",".ToCharArray() == ','` – spender Dec 17 '14 at 15:51
  • If you are starting to make up your own types (list doesnt have a string, string overload.) You might as well just make your own class with properties to hold the data. – crthompson Dec 17 '14 at 15:53
  • 1
    You can't have `List`. – Habib Dec 17 '14 at 15:54
  • Maybe List>? Don't think there is a restriction on unique key, but could be wrong. – Robert Dec 17 '14 at 15:55
  • `Pete` if you want to use something that would be equivalent to `List` then you can use an `Immutable Struct` [C# SO Immutable Struct List](http://stackoverflow.com/questions/19231460/define-a-list-like-listint-string) – MethodMan Dec 17 '14 at 15:57
  • I think this answer may help: http://stackoverflow.com/a/4806256/417579 – Robert Dec 17 '14 at 16:02
  • @spender Technically, `",".ToCharArray != ','`, but `",".ToCharArray().SequenceEqual(new[] { ',' })` ... but I get your point. – Anthony Dec 17 '14 at 16:12

2 Answers2

12
ILookup<string,string> myLookup = 
    files.Zip(rReferences, (s, i) => new { s, i })
         .ToLookup(item => item.s, item => item.i);

will create a Dictionary-like structure that allows multiple values per key.

So

IEnumerable<string> foo = myLookup["somestring"];
spender
  • 117,338
  • 33
  • 229
  • 351
0

A List containing elements with two strings each is simplest implemented with a

List<T>

and

T == Tuple<string,string>

Then use a loop to build your list from the two arrays:

string[] s1 =
{
    "1", "2"
};
string[] s2 =
{
    "a", "b"
};

var jcList = new List<Tuple<string,string>>();

for (int i = 0; i < s1.Length; i++)
{
    jcList.Add(Tuple.Create(s1[i], s2[i]));
}

or using LINQ:

var jcList = s1.Select((t, i) => Tuple.Create(t, s2[i])).ToList();
DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • 2
    with just code the OP can't understand what you are doing. Add some documentation to it – Mivaweb Dec 17 '14 at 15:58
  • `-1` LOL I totally agree `VDesign` wow talk about over thinking in my opinion I would not skin my cat this way.. sorry – MethodMan Dec 17 '14 at 15:59
  • FYI instad of `new Tuple(s1[i], s2[i])` you can use `Tuple.Create(s1[i], s2[i])`. – juharr Dec 17 '14 at 16:02
  • 2
    please note: with spender's Lookup any search will be (much) faster than with this simple flat List – DrKoch Dec 17 '14 at 16:36