To’plam elementlarini IEnumerable ga o’zlashtiradi va hosil bo’lgan ketma – ketlikni bitta kesamdek tasvirlaydi.
Method ko’rinishi:
staticvoidMain(string[] args) {List<string> strList =newList<string>() { "Jasurbek","Xasanboyev" };var methodResult =strList.SelectMany(x => x).ToList();foreach (var item in methodResult) {Console.Write(item +" "); } //Output: J a s u r b e k X a s a n b o y e v }
Query ko’rinishi o`zi aslida vajud emas lekin quydagi ko’rinishda ifodalash mumkin:
staticvoidMain(string[] args) {List<string> strList =newList<string>() { "Jasurbek","Xasanboyev" };var methodResult =from res in strListfrom ch in resselect ch;foreach (var item in methodResult) {Console.Write(item +" "); } //Output: J a s u r b e k X a s a n b o y e v }
Asosan quydagi holatlar uchun ko’proq ishlatiladi:
classProgram {staticvoidMain(string[] args) {List<Employee> employees =newList<Employee>() { new Employee(){Id = 1, Name = "Muhammadkarim", Email = "muhammadkarim@gmail.com", Programming = new List<string>(){"PHP", "C#", "SQL"} },
new Employee(){Id = 2, Name = "Jasurbek", Email = "jasur@gmail.com", Programming = new List<string>(){"C++", "C#", "Arduino"}},
new Employee(){Id = 3, Name = "Xondamir", Email = "xondamir", Programming = new List<string>(){"C#", "Python", "MVC"}}
};var MethodSyntax =employees.SelectMany(x =>x.Programming);foreach (var item in MethodSyntax) {Console.WriteLine(item); } //Output: PHP, C#, SQL, C++, C#, Arduino, C#, Python, MVC } }classEmployee {publicint Id { get; set; }publicstring Name { get; set; }publicstring Email { get; set; }publicList<String> Programming { get; set; } }
SelectMany bizga asosan ichma – ich ro’yhatlar bilan ishlashda qo’l keladi. Query syntaxdan ko’ra method syntaxdan foydalanishni tavsiya beraman chunki query uchun SelectMany yo’q ichma ich tanlab kirishga to’g’ri keladi.