Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining lists in linq

Tags:

c#

linq

in linq, is it possible to combine many lists (of the same type), such that two lists,

list 1 = {a,b,c} and list 2 = {x,y,z}

turns into {[1,a] , [1,b] , [1,c] , [2,x] , [2,y] , [2,z] }

where [] represents a pair containing a "list identifier"

The problem is from having decks of arbitrary cards, where each deck is a list in a collection of lists.

I'm trying to create a query such that I can select only cards in a certain deck, or cards similar to 2 or more decks.

This is probably a duplicate question, but I don't know how to search for the question further then I already have.

like image 736
Ryan Leach Avatar asked Dec 09 '25 00:12

Ryan Leach


1 Answers

List<List<int>> lists;

var combined = lists.Select((l, idx) => new { List = l, Idx = idx })
    .SelectMany(p => p.List.Select(i => Tuple.Create(p.Idx + 1, i)));
like image 65
Lee Avatar answered Dec 11 '25 12:12

Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!