Pregunta

I have two lists, one that store a person main Info and the other that Dynamically store the details. but I need a list to mix both of them and add a sequential number to the output list.

E.G

MAIN

ID --- NAME   ---  AGE
 1 --- Jack   ---   23
 2 --- Robert ---   30
 3 --- Lee    ---   15

DETAILS

ID -- MAINLinK -- Details
1  --    1     --  Tall         <= Jack
2  --    1     --  Thin         <= Jack
3  --    1     --  Blonde       <= Jack
4  --    2     --  Fat          <= Robert
5  --    3     --  Chinesse     <= Lee  
6  --    3     --  skinny       <= Lee

Required List:

Id(MAIN) -- SequentialNUM  -- Name(MAIN)
1        --       1        --  Jack   
1        --       2        --  Jack
1        --       3        --  Jack
2        --       1        --  Robert
3        --       1        --  Lee
3        --       2        --  Lee

the Sequential Number is the one I really need...

NOTE: this is just a simple example to get what I really need and I can't find the way to just put in a single Linq statement to do this this without adding loops and create more objects.

thanks in advance.

¿Fue útil?

Solución

var list = MainList.Join(DetailsList, x=>x.ID, x=>x.MAINLink,(x,y)=> new {x,y})
                   .GroupBy(a=>a.x.ID)
                   .Select(g=>g.Select((k,i)=>new {k,i}))
                   .SelectMany(a=>a)
                   .Select((a)=> new {
                                     Main = a.k.x,
                                     SequentialNUM = a.i + 1,
                                     Detail = a.k.y
                                   })
                   .OrderBy(x=>x.Main.Id);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top