Monday, 9 September 2013

LINQ/LAMBDA Query Example

How to get the Value of attribute from XML using XDocument class
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

How to perform group by in LINQ/LAMBDA
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.Keyi.Count));

     Console.WriteLine("\n=========Using LAMDA===========");
input.GroupBy(g => g).Select(i => new { Key = i.KeyCount = i.Count() })
.ToList()
.ForEach(i => Console.WriteLine("Number of {0} is {1}"i.Keyi.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 = 0i < input.Lengthi++)
            {
                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.DeptNameEmployeesName = (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 = { 30251590994275 };
bool isMultiple = MultipleTester(numbers5);
Console.WriteLine("isMultiple= " + isMultiple);
Console.ReadLine();

private static bool MultipleTester(int[] numbersint 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

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (49) CD (1) CI (2) CloudComputing (2) Coding (8) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (19) Entity Framework (4) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives