using System;
using System.Linq;
class Program
{
public static void Main()
{
int[] nums = { 10, -6, 3, 0, -4, 25 };
// Query that selects only positive numbers.
var result = from n in nums
where n > 0
select n;
Console.Write("The positive values in nums: ");
// Execute the query.
foreach (int i in result)
{
Console.Write(i + " ");
}
Console.ReadLine();
}
}
foreach (int i in result)
{
Console.Write ( i + " ");
}
using System;
using System.Linq;
class Program
{
public static void Main()
{
int[] nums = { 6, -2, 30, -3, 0, -8, 12, 7, 8, 9, 100 };
// Query for obtains positive values less than 10.
var result = from n in nums
where n > 0
where n < 10
select n;
Console.Write("The positive values less than 10: ");
// Execute the query.
foreach (int i in result)
{
Console.Write(i + " ");
}
Console.ReadLine();
}
}
Type | Standard Query Operators |
---|---|
Filter | Where |
Sorting | OrderBy, OrderByDescending, Reverse |
Grouping | GroupBy |
Join | GroupJoin, Join |
Projection | Select, SelectMany |
Quantifiers | All, Any, Contains |
Set | Except, Intersect, Union , Distinct |
Partitioning | SkipWhile, Take, TakeWhile, Skip |
Concatenation | Concat |
Equality | SequenceEqual |
Generation | DefaultEmpty, Empty, Range, Repeat |
Aggregation | Aggregate, Average, Count, LongCount, Max, Min, Sum |
using System;
using System.Collections.Generic;
using System.Linq;
class Employee
{
public int EmployeeID { get; set; }
public string EmpName { get; set; }
public int Age { get; set; }
}
class Program
{
public static void Main()
{
List<Employee> empList = new List<Employee>()
{
new Employee() { EmployeeID = 1, EmpName = "Raj", Age = 28 } ,
new Employee() { EmployeeID = 2, EmpName = "Rajesh", Age = 16 } ,
new Employee() { EmployeeID = 3, EmpName = "Sunder", Age = 35 } ,
new Employee() { EmployeeID = 4, EmpName = "Ram" , Age = 20 } ,
new Employee() { EmployeeID = 5, EmpName = "Ronit" , Age = 38 }
};
var empResult = from s in empList
where s.Age > 12 && s.Age < 30 && s.EmpName.StartsWith("R") &&
s.EmpName.EndsWith("j")
select s.EmpName;
foreach (var emp in empResult)
{
Console.WriteLine(emp);
}
Console.ReadLine();
}
}
var empResult = from s in empList
orderby s.EmpName descending
select s;
{
new Employee() { EmployeeID = 1, EmpName = "Raj", Age = 28 } ,
new Employee() { EmployeeID = 2, EmpName = "Rajesh", Age = 28 } ,
new Employee() { EmployeeID = 3, EmpName = "Sunder", Age = 35 } ,
new Employee() { EmployeeID = 4, EmpName = "Ram" , Age = 20 } ,
new Employee() { EmployeeID = 5, EmpName = "Ronit" , Age = 20 }
};
var empResult = from s in empList group s by s.Age;
foreach (var emp in empResult)
{
Console.WriteLine("Employee Age Group = "+emp.Key);
foreach (Employee e in emp)
Console.WriteLine("Student Name: {0}", e.EmpName);
}
using System;
using System.Linq;
class Program
{
public static void Main()
{
int [ ] nums ={10,20,30,40,50,1,2,3,4,5};
Console.WriteLine("Total count = "+nums.Count());
Console.WriteLine("Average = "+nums.Average());
Console.WriteLine("Summation = "+nums.Sum());
Console.WriteLine("Max value = "+nums.Max());
Console.WriteLine("Min Value = "+nums.Min());
Console.ReadLine();
}
}
using System;
using System.Linq;
class Program
{
public static void Main()
{
string [ ] List1 = { "One", "Two", "three", "Four", "Six" };
string [ ] List2 = { "Two", "THREE", "Four", "Five" };
Console.WriteLine("Unique elements in both list\n");
var result = List1.Union(List2);
foreach (string str in result)
{
Console.WriteLine(str);
}
Console.WriteLine("\n Common elements in both list\n\n");
var IntResult = List1.Intersect(List2);
foreach (string str in IntResult)
{
Console.WriteLine(str);
}
Console.WriteLine("\n Element in list1 after minus from list2 \n");
var ExceptResult = List1.Except(List2);
foreach (string str in ExceptResult)
{
Console.WriteLine(str);
}
Console.ReadLine();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static void Main()
{
List<int> intList = new List<int> {10,20,30,40,50,60 };
List<string> strList = new List<string>()
{ "One","Two", "Three", "Four", "Five","Six" };
Console.WriteLine("Skips the first three elemts from integer list");
var firstList = intList.Skip(3);
foreach(var item in firstList)
{
Console.WriteLine(item);
}
Console.WriteLine("SkipsWhile method (Length < 4) by string list") ;
var secondList = strList.SkipWhile(s => s.Length < 4);
foreach (string str in secondList)
{
Console.WriteLine(str);
}
Console.WriteLine("Takes the first three elements from integer list");
var thirdList = intList.Take(3);
foreach (var item in thirdList)
{
Console.WriteLine(item);
}
Console.WriteLine("TakeWhile method (Length < 4) by string list");
var fourthList = strList.TakeWhile(s => s.Length < 4);
foreach (string str in fourthList)
{
Console.WriteLine(str);
}
Console.ReadLine();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
class Employee
{
public int EmployeeID { get; set; }
public string EmpName { get; set; }
public int Age { get; set; }
}
class Program
{
public static void Main()
{
List<Employee> empList = new List<Employee>()
{
new Employee() { EmployeeID = 1, EmpName = "Raj", Age = 28 } ,
new Employee() { EmployeeID = 2, EmpName = "Rajesh", Age = 16 } ,
new Employee() { EmployeeID = 3, EmpName = "Sunder", Age = 35 } ,
new Employee() { EmployeeID = 4, EmpName = "Ram" , Age = 20 } ,
new Employee() { EmployeeID = 5, EmpName = "Ronit" , Age = 38 }
};
Console.WriteLine("Does All employee satisfies the condition. Age > 12 && Age < 20");
bool emp = empList.All(s => s.Age > 12 && s.Age < 20);
Console.WriteLine(emp);
Console.WriteLine("Does Any employee satisfies the condition Age > 12 && Age < 20");
bool anyEmp = empList.Any(s => s.Age > 12 && s.Age < 20);
Console.WriteLine(anyEmp);
Console.ReadLine();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static void Main()
{
List<int> intList = new List<int>() { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
List<string> strList = new List<string>() { "One", "Two", "Three", null, "Five" };
Console.WriteLine("1st Element in intList: {0}", intList.ElementAt(0));
Console.WriteLine("1st Element in strList: {0}", strList.ElementAt(0));
Console.WriteLine("4th Element in intList: {0}", intList.ElementAtOrDefault(3));
Console.WriteLine("4th Element in strList: {0}", strList.ElementAtOrDefault(3));
Console.WriteLine("11th Element in intList: {0} : default int value",
intList.ElementAtOrDefault(11));
Console.WriteLine("11th Element in strList: {0} : default string value is (null)",
strList.ElementAtOrDefault(11));
Console.WriteLine("First Even Element in intList: {0}", intList.First(i => i % 2 == 0));
Console.WriteLine("First Element in strList: {0}", strList.First());
Console.WriteLine("Last Element in intList: {0}", intList.Last());
Console.WriteLine("Last Element in strList: {0}", strList.Last());
Console.ReadLine();
}
}