Can you combine multiple lists with LINQ?

Say I have two lists:

var list1 = new int[] {1, 2, 3}; var list2 = new string[] {"a", "b", "c"};

Is it possible to write a LINQ statement that will generate the following list:

var result = new []{ new {i = 1, s = "a"}, new {i = 1, s = "b"}, new {i = 1, s = "c"}, new {i = 2, s = "a"}, new {i = 2, s = "b"}, new {i = 2, s = "c"}, new {i = 3, s = "a"}, new {i = 3, s = "b"}, new {i = 3, s = "c"} };


Edit: I forgot to mention I didn't want it in query syntax. Anyway, based on preetsangha's answer I've got the following:

var result = list1.SelectMany(i => list2.Select(s => new {i = i, s = s}));

-------------Problems Reply------------

preetsangha's answer is entirely correct, but if you don't want a query expression then it's:

var result = list1.SelectMany(l1 => list2, (l1, l2) => new { i = l1, s = l2} );

(That's what the compiler compiles the query expression into - they're identical.)

var result = from l1 in list1
from l2 in list2
select new { i = l1, s = l2};

Category:c# Views:0 Time:2008-11-14
Tags: .net c# linq

Related post

  • LINQ C# - Combining multiple groups 2009-09-08

    LINQ Groupby query creates a new group for each unique key. I would like to combine multiple groups into a single group based on the key value. e.g. var customersData = new[] { new { id = 1, company = "ABC" }, new { id = 2, company = "AAA" }, new { i

  • Can you combine multiple images into a single one using JavaScript? 2008-10-01

    I am wondering if there is a way to combine multiple images into a single image using only JavaScript. Is this something that Canvas will be able to do. The effect can be done with positing, but can you combine them into a single image for download?

  • How do I combine multiple BIRT reports 2008-10-23

    We currently have a whole suite of report designs that cover various parts of our app, and these reports are generated on demand by our users. I want to be able to bundle up several of these reports into a single report to return to the user. I initi

  • SQL: Combine multiple dynamic resultsets in one stored procedure 2008-12-29

    We're having a small issue and could use some help - we have the need to combine multiple resultsets from one stored procedure into one resultset (the limitation of our Java reporting framework). We've looked at Union, etc. but the problem is that th

  • Combining multiple config files in Visual Studio 2009-01-21

    I have a solution in Visual Studio 2008 which contains multiple C# projects. Each project has it's own config file (App.config and Settings.settings). This makes sense architecturally as each module is autonomous and is used in a number of different

  • Oracle: Combine multiple results in a subquery into a single comma-separated value 2009-01-29

    This question already has an answer here: How can I combine multiple rows into a comma-delimited list in Oracle? [duplicate] 11 answers I'm trying to convert a single-columned subquery into a command-separated VARCHAR-typed list of values. This is id

  • How do I collect and combine multiple arrays for calculation? 2009-06-26

    I am collecting the values for a specific column from a named_scope as follows: a = survey_job.survey_responses.collect(&:base_pay) This gives me a numeric array for example (1,2,3,4,5). I can then pass this array into various functions I have cr

  • combining multiple .js files into one in a build process 2009-06-27

    Any tips on combining multiple .js files into 1 (for a build process). Will yuicompressor do this? --------------Solutions------------- YUI Compressor can do this very easily, just send in the file names into YUI as arguments and it will output them

  • How to combine multiple Excel files into single Excel workbook with multiple sheets? 2009-06-30

    I need to develop a service (either WCF or Java) that can take multiple single-sheet Excel files and combine them into a single Excel workbook with multiple sheets. The Excel files are actually exported from crystal reports as individual files. Follo

  • How do you group by multiple columns in LINQ TO SQL? 2009-07-10

    How do you group by multiple columns in LINQ TO SQL? db.Table.GroupBy(a => a.column1.ToString() + a.column2.ToString()) It seems ugly and with poor performance, and I don't even know if it works. Which is the right way to do it? --------------Solu

  • Combine Multiple Predicates 2009-08-08

    Is there any way in c# .NET 2.0! to combine multiple Predicates? Let's say I have the following code. List<string> names = new List<string>(); names.Add("Jacob"); names.Add("Emma"); names.Add("Michael"); names.Add("Isabella"); names.Add("

  • Combine multiple Ellipses 2009-10-20

    I need a way to combine multiple EllipseGeometry to make a union between them, but CombineGeometry class only can combine 2 Geometries. The ideal way is to have a class similar CombineGeometry that accepts more than Geometry1 and Geometry2 shapes. An

  • Return Multiple Columns in Linq to Sql? 2009-12-11

    How do I return multiple columns with linq to sql in C#? I tried to end my query with select new { A.Product, A.Qty }; but this returns some anonymous type and I am not sure what the heck what to do with this, How to return it and how to extract info

  • How to combine multiple images fast for page views counter 2009-12-15

    How can I combine multiple images, such as base image with logo and number of digits images to display graphical counter with pageviews count, updated dynamically? It should be very fast, with thousands of renders per second. User should see counter

  • How I can combine multiple elements to one validator? 2009-12-29

    How I can combine multiple form elements to one validator? I have address information which consists of Street address ZIP code Post office If I add validator to each of them as streetValidator, zipCodeValidator, postOfficeValidator I end up with pro

  • Combine multiple twitter feeds into one list with PHP? 2010-04-23

    How would I go about combining multiple users twitter feeds into one list and display them on my page? --------------Solutions------------- You'd have to go in, I'd say, three steps : Get the feed of each one of the users Those are available as RSS f

  • Algorithm - combine multiple lists, resulting in unique list and retaining order 2010-04-28

    I want to combine multiple lists of items into a single list, retaining the overall order requirements. i.e.: 1: A C E 2: D E 3: B A D result: B A C D E above, starting with list 1, we have ACE, we then know that D must come before E, and from list 3

  • How to combine multiple live(Video) streams in single stream? so that output will be multiple (splitted)videos in single video? 2010-04-30

    I am very new to the streaming. My requirement is to combine multiple videos/streams in to single video. So output will be single video showing multiple videos simultaneously in single player(Like security camera video). Here is my sample code--- IGr

  • How to combine multiple Hadoop MapReduce Jobs into one? 2010-06-29

    I have a massive amount of input data (that's why I use Hadoop) and there are multiple tasks that can be solved with various MapReduce steps of which the first mapper needs all the data as input. My goal: Compute these different tasks as fast as poss

Copyright (C), All Rights Reserved.

processed in 0.142 (s). 11 q(s)