-3

I have several lists of several different classes (R, L , S):

List<R> someListR = ...;
List<L> someListL = ...;
List<S> someListS = ...;

Each class has an integer Id field but they all have unique Ids.

I want to convert someList3 to a Dictionary<string, S> with the key being:

R.Id + ":" + L.Id

EDIT:

The Lists sizes can be different and in that case I would create a key.

For example a List R has:

R1.Id = 111;
R2.Id = 222;
R3.Id = 333;

and List L:

L1.Id = 123;
L2.Id = 234;
L3.Id = 345;
L4.Id = 456;

I want to create a key=> value dictionary like this:

111:123 => S1
222:234 => S2
333:345 => S3
9
  • Read the golden rule msmvps.com/blogs/jon_skeet/archive/2010/08/29/…
    – L.B
    Feb 14, 2014 at 21:46
  • The question is not clear. Are the Id in all three classes the same? Show simplifies sample input and expected output. Feb 14, 2014 at 21:46
  • No the Ids in all three classes are different.
    – stackErr
    Feb 14, 2014 at 21:48
  • Does each list have the same count of items? If L has less items than R, should a Key be derived?
    – StingyJack
    Feb 14, 2014 at 21:51
  • @stackErr so you want to throw away Id's of S and replace each of them with R.Id + ":" + L.Id ? are the sizes of lists are the same? Feb 14, 2014 at 21:51

1 Answer 1

3

I'm still not sure I get your question, but looks like you need Zip method:

var result = listL.Zip(listR, (l,r) => new { l, r })
                  .Zip(listS, (x, s) => new { x.l, x.r, x.s })
                  .ToDictionary(x => x.l.ID + ":" + x.r.ID, x => x.s);

Not the answer you're looking for? Browse other questions tagged or ask your own question.