Linq
The types of LINQ are mentioned below in brief.
LINQ to Objects
LINQ to XML(XLINQ)
LINQ to DataSet
LINQ to SQL (DLINQ)
LINQ to Entities
Apart from the above, there is also a LINQ type named PLINQ which is Microsoft’s parallel LINQ.
var list = (from e in employees
join d in departments on e.DepartmentId equals d.DepartmentId
select new
{
EmployeeName = e.EmployeeName,
DepartmentName = d.Name,
});
foreach(var e in list)
{
Console.WriteLine("Employee name={0},Department Name={1}",e.EmployeeName,e.DepartmentName);
}
List<string> phrases = new List<string> { "an apple a day", "the quick brown fox" };
var query = from phrase in phrases
from word in phrase.Split(' ')
select word;
foreach(var s in query)
{
Console.WriteLine(s);
}
Console.ReadLine();
static void Main(string[] args)
{
int[] num = { -20, 12, 6, 10, 0, -3, 1 };
var posNum = from n in num
orderby n
select n;
Console.WriteLine("Values in ascending order: ");
foreach(int i in posNum)
{
Console.WriteLine(i);
}
var posNumsDesc = from n in num
orderby n descending
select n;
Console.WriteLine("\nValues in descending order: ");
foreach(int i in posNumsDesc)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
static void Main(string[] args)
{
List<int> numbers = new List<int>() { 35, 44, 200, 84, 3987, 4, 199, 329, 446, 208 };
IEnumerable<IGrouping<int, int>> query = from num in numbers
group num by num % 2;
foreach(var group in query)
{
Console.WriteLine(group.Key == 0 ? "\nEven numbers: " : "\nOdd numbers: ");
foreach(var i in group)
{
Console.WriteLine(i);
}
}
Console.ReadLine();
}
class Program
{
static void Main(string[] args)
{
Plant[] plants = new Plant[]
{
new CarnivorousPlant
{
Name="Venus Fly Trap",
TrapType="Snap Trap"
},
new CarnivorousPlant
{
Name="Pitcher Plant",
TrapType="Pitfall Trap"
},
new CarnivorousPlant
{
Name="Sundew",
TrapType="Flaypaper Trap"
},
new CarnivorousPlant
{
Name="WaterWheel Plant",
TrapType="Snap Trap"
}
};
var query = from CarnivorousPlant cPlant in plants
where cPlant.TrapType == "Snap Trap"
select cPlant;
foreach(var q in query)
{
Console.WriteLine("Name={0},Trap Type={1}", q.Name, q.TrapType);
}
Console.WriteLine("\nPress any key to continue");
Console.ReadLine();
}
}
class Plant
{
public string Name { get; set; }
}
class CarnivorousPlant:Plant
{
public string TrapType { get; set; }
}
class Program
{
static void Main(string[] args)
{
Pet[] cats = GetCats();
Pet[] dogs = GetDogs();
IEnumerable<string> query = cats.Select(cat => cat.Name).Concat(dogs.Select(dog => dog.Name));
foreach(var e in query)
{
Console.WriteLine("Name={0}", e);
}
Console.WriteLine("\nPress any key to continue");
Console.ReadLine();
}
static Pet[] GetCats()
{
Pet[] cats = {new Pet { Name="Barley",Age=8},
new Pet{Name="Boots",Age=4},
new Pet{Name="Whiskers",Age=1}};
return cats;
}
static Pet[] GetDogs()
{
Pet[] dogs =
{
new Pet{Name="Bounder",Age=3},
new Pet{Name="Snoopy",Age=14},
new Pet{Name="Fido",Age=9}
};
return dogs;
}
}
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}