public static void Main(string[] args) { //// Example 1 List <string> nameList = new List <string>() { "Kamal", "Ahmed" }; IEnumerable <char> querySyntax1 = from str in nameList from ch in str select ch; IEnumerable <char> methodSyntax1 = nameList.SelectMany(x => x); //// Example 2 List <string> methodSyntax2 = Student.GetStudents().SelectMany(std => std.Programming).ToList(); IEnumerable <string> querySyntax2 = from std in Student.GetStudents() from program in std.Programming select program; //// Example 3 List <string> methodSyntax3 = Student.GetStudents() .SelectMany(std => std.Programming) .Distinct() .ToList(); IEnumerable <string> querySyntax3 = (from std in Student.GetStudents() from program in std.Programming select program).Distinct().ToList(); //// Example 4 var methodSyntax4 = Student.GetStudents() .SelectMany(std => std.Programming, (student, program) => new { StudentName = student.Name, ProgramName = program } ) .ToList(); var querySyntax4 = (from std in Student.GetStudents() from program in std.Programming select new { StudentName = std.Name, ProgramName = program }).ToList(); foreach (var item in methodSyntax4) { Console.WriteLine(item.StudentName + " => " + item.ProgramName); } }
public static void Main(string[] args) { ///// Example 1 List <int> intList = new List <int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; IEnumerable <int> methodSyntax1 = intList.Where(num => num > 5); IEnumerable <int> querySyntax1 = from num in intList where num > 5 select num; ///// Example 2 var querySyntax2 = from number in intList.Select((num, index) => new { Numbers = num, IndexPosition = index }) where number.Numbers % 2 != 0 select new { Number = number.Numbers, IndexPosition = number.IndexPosition }; var methodSyntax2 = intList.Select((num, index) => new { Numbers = num, IndexPosition = index }) .Where(x => x.Numbers % 2 != 0) .Select(data => new { Number = data.Numbers, IndexPosition = data.IndexPosition }); ///// Example 3 var querySyntax3 = (from student in Student.GetStudents() where student.Salary >= 50000 && student.Technology != null select new { StudentName = student.Name, Gender = student.Gender, MonthlySalary = student.Salary / 12 }).ToList(); var methodSyntax3 = Student.GetStudents() .Where(student => student.Salary >= 50000 && student.Technology != null) .Select(student => new { EmployeeName = student.Name, Gender = student.Gender, MonthlySalary = student.Salary / 12 }) .ToList(); }
public static void Main(string[] args) { var methodSyntax = Student.GetStudents() // Type is IOrderedEnumerable<Student> .Where(std => std.Gender.ToUpper() == "MALE" || std.Gender.ToUpper() == "FEMALE") .OrderBy(x => x.Name) //.OrderByDescending(x => x.Name). Will be ordered by descending .ThenByDescending(x => x.Salary) .ThenBy(x => x.Gender); var querySyntax = from student in Student.GetStudents() where student.Gender.ToUpper() == "MALE" // Type is IOrderedEnumerable<Student> orderby student.Name, // ascending is optional student.Salary descending, student.Gender ascending select student; foreach (var item in methodSyntax) { Console.WriteLine(item.Name); } }
public static void Main(string[] args) { //////// Select All Property of Student var querySyntax1 = (from std in Student.GetStudents() select std).ToList(); var methodSyntax1 = Student.GetStudents().ToList(); //////// Select Single Property of Student var querySyntax2 = (from std in Student.GetStudents() select std.ID).ToList(); var methodSyntax2 = Student.GetStudents().Select(emp => emp.ID); //////// Select Multiple Property of Student var querySyntax3 = (from std in Student.GetStudents() select new Student() { FirstName = std.FirstName, LastName = std.LastName }).ToList(); var methodSyntax3 = Student.GetStudents(). Select(std => new Student() { FirstName = std.FirstName, LastName = std.LastName }).ToList(); //////// Select Multiple Property of Student to another New Class var querySyntax4 = (from std in Student.GetStudents() select new StudentInfo() { FirstName = std.FirstName, LastName = std.LastName }).ToList(); var methodSyntax4 = Student.GetStudents(). Select(std => new StudentInfo() { FirstName = std.FirstName, LastName = std.LastName }).ToList(); //////// Select Multiple Property of Student to Anonymous Type var querySyntax5 = (from std in Student.GetStudents() select new { FirstName = std.FirstName + " World ", // Can perform manipulation LastName = std.LastName }).ToList(); var methodSyntax5 = Student.GetStudents(). Select(std => new { FirstName = std.FirstName + " World ", LastName = std.LastName }).ToList(); //////// Select data with index value var querySyntax6 = (from std in Student.GetStudents().Select((value, index) => new { value, index }) select new { IndexPosition = std.index, FullName = std.value.FirstName + " " + std.value.LastName, }).ToList(); var methodSyntax6 = Student.GetStudents(). Select((std, index) => new { IndexPosition = index, FullName = std.FirstName + " " + std.LastName, }).ToList(); foreach (var emp in methodSyntax6) { Console.WriteLine($" Position {emp.IndexPosition} Name : {emp.FullName}"); } }