合并不等长度列表

本文关键字:列表 合并 | 更新日期: 2023-09-27 18:05:30

有两个长度不等的列表,我想合并成一个列表。

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();

数据源包含值(A,B,C)

datafxed包含值(D,E,F,G,H)

预期结果

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

尝试使用.Zip(),但无法处理大小不等的列表。

合并不等长度列表

试试这个

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();
        }
    }
}