Merge multiple Lists into one List with LINQ

前端 未结 8 1801
南方客
南方客 2020-11-28 12:50

Is there a slick way to merge multiple Lists into a single List using LINQ to effectively replicate this?

public class RGB
{
    public int Red { get; set; }         


        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 13:14

    Yes - you can do it like this:

    List red = new List { 0x00, 0x03, 0x06, 0x08, 0x09 };
    List green = new List { 0x00, 0x05, 0x06, 0x07, 0x0a };
    List blue = new List { 0x00, 0x02, 0x03, 0x05, 0x09 };
    
    List colors = Enumerable
        .Range(0, red.Count)
        .Select(i => new RGB(red[i], green[i], blue[i]))
        .ToList();
    

提交回复
热议问题