How to get the Value of attribute from XML using XDocument class
Output will be
How to perform group by in LINQ/LAMBDA
string inputXml = @"<?xml version='1.0' encoding='UTF-8'?>
<Countries>
<State Name = 'Karnataka'>
<City Name='Bangalore' />
<City Name= 'Guledgudda' />
<City Name= 'Hubli' />
<City Name= 'Tumkur' />
</State>
</Countries>";
XDocument countrySource = XDocument.Parse(inputXml);
//The query
countrySource.Descendants("State").SelectMany(i => i.Elements())
.ToList().ForEach(i => Console.WriteLine((string)i.Attribute("Name")
));
Output will be
Bangalore
Guledgudda
Hubli
Tumkur
var input = new string[] { "Apple", "Banana", "Mango", "Apple",
"Orange", "Mango", "Strawberry", "Apple" };
Console.WriteLine("=========Using LINQ===========");
(from a in input group a by a into g
select new
{
Key = g.Key,
Count = g.Count()
})
.ToList().ForEach(i => Console.WriteLine("Number of {0} is {1}",
i.Key, i.Count));
Console.WriteLine("\n=========Using LAMDA===========");
input.GroupBy(g => g).Select(i => new { Key = i.Key, Count = i.Count() })
.ToList()
.ForEach(i => Console.WriteLine("Number of {0} is {1}", i.Key, i.Count));
Output will be
How to assign a computed value to the same array back
var input = new string[] { "Apple", "Banana", "Mango", "Apple", "Orange",
"Mango", "Strawberry", "Apple" };
input = (from a in input group a by a into g where g.Count() >= 2
select g.Key + " ( " + g.Count() + " )").ToArray();
for (int i = 0; i < input.Length; i++)
{
Console.WriteLine(input[i]);
}
Output will be
Apple (3)
Mango (2)
Write a code snippet for Left outer join using LINQ
class Employee
{
public string EmpName { get;
set; }
public int EmpAge { get; set; }
public string EmpDeptID { get;
set; }
}
class Department
{
public string DeptID { get; set; }
public string DeptName { get;
set; }
}
Employee
objEmp1 = new Employee
{ EmpName = "Naga", EmpAge = 29,
EmpDeptID = "1" };
Employee
objEmp2 = new Employee
{ EmpName = "Sundar", EmpAge = 30,
EmpDeptID = "2" };
Employee
objEmp3 = new Employee
{ EmpName = "Siva", EmpAge = 28,
EmpDeptID = "3" };
Employee
objEmp4 = new Employee
{ EmpName = "Shankar", EmpAge =
31, EmpDeptID = "4" };
Department
objDept1 = new Department
{ DeptID = "1", DeptName = "IT" };
Department
objDept2 = new Department
{ DeptID = "2", DeptName = "Admin" };
Department
objDept3 = new Department
{ DeptID = "3", DeptName = "Testing" };
Department
objDept4 = new Department
{ DeptID = "4", DeptName = "HR" };
Department
objDept5 = new Department
{ DeptID = "5", DeptName = "Sales" };
//Creating
List of Objects
List<Employee> employees = new
List<Employee>
{ objEmp1, objEmp2, objEmp3, objEmp4 };
List<Department> depts = new
List<Department>
{ objDept1, objDept2, objDept3, objDept4, objDept5 };
//Left Side Department Right side Employee
var DeptEmpCollection = from dept in depts
join emp in employees on dept.DeptID equals emp.EmpDeptID into de
from Employees in de.DefaultIfEmpty()
select new { dept.DeptName, EmployeesName = (Employees == null ?
"--No Employee--" : Employees.EmpName) };
foreach (var EmpDept in DeptEmpCollection)
{
Console.WriteLine("Dept {0}, EmpName {1}", EmpDept.DeptName,
EmpDept.EmployeesName);
}
Console.Read();
Output will be
Given an array of numbers, find if ALL numbers are a multiple of a provided number [5]
int[] numbers = { 30, 25, 15, 90, 99, 42, 75 };
bool isMultiple = MultipleTester(numbers, 5);
Console.WriteLine("isMultiple= " + isMultiple);
Console.ReadLine();
private static bool MultipleTester(int[] numbers, int divisor)
{
bool isMultiple = numbers.All(number => number % divisor == 0);
//Note:- if you want to check any number is divisible by 5 then
//use numbers.any()
return isMultiple;
}
Output will be
isMultiple= False
0 comments:
Post a Comment