C# में Where क्लॉज का उपयोग करके संग्रह फ़िल्टर करें। एकल क्वेरी अभिव्यक्ति में एकाधिक where क्लॉज हो सकते हैं।
सबसे पहले, एक संग्रह सेट करें:
IList<Employee> employee = new List<Employee>() { new Employee() { EmpID = 1, EmpName = "Tom", EmpMarks = 90, Rank = 8} , new Employee() { EmpID = 2, EmpName = "Anne", EmpMarks = 60, Rank = 21 } , new Employee() { EmpID = 3, EmpName = "Jack", EmpMarks = 76, Rank = 18 } , new Employee() { EmpID = 4, EmpName = "Amy" , EmpMarks = 67, Rank = 20} , };अब, रैंक 5 से अधिक और 10 से कम वाले कर्मचारी को प्राप्त करने के लिए एकाधिक where क्लॉज का उपयोग करें।
var res = from e in employee where e.Rank > 5 where e.Rank < 10 select e;
निम्नलिखित कोड है:
उदाहरण
using System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { IList<Employee> employee = new List<Employee>() { new Employee() { EmpID = 1, EmpName = "Tom", EmpMarks = 90, Rank = 8} , new Employee() { EmpID = 2, EmpName = "Anne", EmpMarks = 60, Rank = 21 } , new Employee() { EmpID = 3, EmpName = "Jack", EmpMarks = 76, Rank = 18 } , new Employee() { EmpID = 4, EmpName = "Amy" , EmpMarks = 67, Rank = 20} , }; var res = from e in employee where e.Rank > 5 where e.Rank < 10 select e; foreach (var emp in res) { Console.WriteLine("नाम: "+emp.EmpName); Console.WriteLine("अंक: "+emp.EmpMarks); } } } public class Employee { public int EmpID { get; set; } public string EmpName { get; set; } public int EmpMarks { get; set; } public int Rank { get; set; } }आउटपुट
नाम: Tom अंक: 90