सूची को समतल करने का अर्थ है सूची<सूची
LINQ में SelectMany का उपयोग अनुक्रम के प्रत्येक तत्व को anIEnumerable
SelectMany का उपयोग करना
उदाहरण
using System;
using System.Collections.Generic;
using System.Linq;
namespace DemoApplication{
public class Program{
static void Main(string[] args){
List<List<int>> listOfNumLists = new List<List<int>>{
new List<int>{
1, 2
},
new List<int>{
3, 4
}
};
var numList = listOfNumLists.SelectMany(i => i);
Console.WriteLine("Numbers in the list:");
foreach(var num in numList){
Console.WriteLine(num);
}
Console.ReadLine();
}
}
} आउटपुट
Numbers in the list: 1 2 3 4
क्वेरी का उपयोग करना
उदाहरण
using System;
using System.Collections.Generic;
using System.Linq;
namespace DemoApplication{
public class Program{
static void Main(string[] args){
List<List<int>> listOfNumLists = new List<List<int>>{
new List<int>{
1, 2
},
new List<int>{
3, 4
}
};
var numList = from listOfNumList in listOfNumLists
from value in listOfNumList
select value;
Console.WriteLine("Numbers in the list:");
foreach(var num in numList){
Console.WriteLine(num);
}
Console.ReadLine();
}
}
} आउटपुट
Numbers in the list: 1 2 3 4