-2

There are 2 lists with the unequal length , i want to combine into single list.

var dataresource = (from det in dbobj.tbls1
                                     where det.WorkTypeId == 1
                                          select new
                                          {
                                              resnum = sowdet.number,


                                          }).ToList();

var datafixed = (from det123 in dbobj.tbls1
                                     where det123.WorkTypeId == 2
                                          select new
                                          {
                                              fixednum = det123.number,

                                          }).ToList();

dataresource contains values (A,B,C)

datafixed contains values (D,E,F,G,H)

Result expected is

Resultlist = 
A D
B E
C F
null G
null H

Tried using .Zip() but unable to handle the list with unequal size.

t3chb0t
  • 16,340
  • 13
  • 78
  • 118
yeshu
  • 1
  • 1
  • A quick google search: http://stackoverflow.com/questions/19522233/how-to-combine-multiple-lists-of-same-or-different-lengths-in-linq, http://stackoverflow.com/questions/1190657/add-two-lists-of-different-length-in-c-sharp – Pau C Sep 04 '16 at 15:08
  • Tried this but the values are repeating and not getting the expected result. – yeshu Sep 04 '16 at 15:19
  • 1
    I think it would be easier without the linq, just iterate the lists and add the to a third one. Anyway, I don't get your "expected result", why are there null values? – Pau C Sep 04 '16 at 15:25

1 Answers1

-1

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> datasource = new List<string>() { "A", "B", "C" };
            List<string> datafixed = new List<string>() { "D", "E", "F", "G", "H" };

            var results1 = datasource.Select((x,i) => new { source = x, fix = i < datafixed.Count ? datafixed[i] : null}).ToList();
            var results2 = datafixed.Select((x, i) => new { source = x, fix = i < datasource.Count ? datasource[i] : null }).ToList();

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20