archangelmihail

LINQ

Feb 12th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. LINQ
  2.  
  3. var innerGroupJoinQuery2 =
  4.     from category in categories
  5.     join prod in products on category.ID equals prod.CategoryID into prodGroup
  6.     from prod2 in prodGroup
  7.     where prod2.UnitPrice > 2.50M
  8.     select prod2;
  9.  
  10. var someCollection = students
  11.                 .Where(st => st.Id > 1)
  12.                 .OrderBy(st => st.Name)
  13.                 .ThenBy(st => st.Courses.Count)
  14.                 .Select(st => new
  15.                 {
  16.                     Name = st.Name,
  17.                     Courses = st.Courses.Count
  18.                 })
  19.                 .ToArray();
  20.  
  21. var projectedItems = students.Select(st => new Student { Name = st.Id.ToString(), Courses = new List<Course>() });
  22.  
  23. var ordered = students.OrderBy(st => st.Courses.Count).ThenBy(st => st.Name);
  24.  
  25. int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  26. var querySmallNums =
  27.             from num in numbers
  28.             where num < 5
  29.             select num;
  30.  
  31.  
  32. public static IEnumerable<Tuple<string, double>> AverageAge(Animal[] animals) //Get every kind of animal in separate group and calculate average
  33.         {
  34.             var averageAges =
  35.                 from animal in animals
  36.                 group animal by animal.GetType() into animalType
  37.                 select new Tuple<string, double>(animalType.Key.Name, animalType.Average(a => a.Age));
  38.  
  39.             return averageAges;
  40.         }
Advertisement
Add Comment
Please, Sign In to add comment