LINQ

1)    What is LINQ?
LINQ is an Acronym of Language Integrated Query thave have a uniform query syntax to query from different-2 Data Source like DataTable, DataSet , XML Docs etc and you can write queries in C# and VB.Net.
LINQ provides a single  or uniform interface to query from Data sources.   

2)    How many types , we can query by LINQ?
There are two types as below
·         Query Syntax or Query Expression Syntax
var data= from a in Customers where a.ID > 10
                        Select a;          

·         Method Syntax or Method Extension Syntax
var data = Customers.Where(a=>a.ID > 10);

3)    What is Anonymous function?
·         Anonymous function are the special type of function that doesn’t have name

4)    What are the query operator that can use in query?
There are various operator provided by LINQ as given below
Below are the list through which we can easily understand this concept
IList<Student> lstStudent = new List<Student>{
            new Student(){ Name="A1",Email="ajay@gmail.com",Address="Delhi",Age=10 },
            new Student(){ Name="A2",Email="majay@gmai1l.com",Address="Saharanpur",Age=12 },
             new Student(){ Name="A1",Email="ajay@gmail112.com",Address="Delhi",Age=14 },
             new Student(){ Name="A5",Email="ajay@gmai2l.com",Address="Saharanpur" ,Age=28},
              new Student(){ Name="A5",Email="ajay@gmai2l.com",Address="Noida" ,Age=30},
            new Student(){ Name="A3",Email="ajay@gmai2l.com",Address="NCR" ,Age=20}
            };
This is a list of students which have Name, Email and Address.
Where – this operator is used to filter query data as you can use as follows  
By Query Syntax
            var whereByQuery = from a in lstStudent
                                             where a.Age > 10
                                                         select a;
By Method Syntax
      var whereByMethod = lstStudent.Where(a => a.Age > 10).ToList();



Orderby – orderby operator is used to sort a list by ascending or descending
By Query Syntax
            var orderby = from a in lstStudent
                          orderby a.Address
                     select a;

            var orderbyDesc = from a in lstStudent
                          orderby a.Address descending
                     select a;
By Method Syntax
            var orderbyMethod = lstStudent.OrderBy(a => a.Age).ToList();
            var orderbyMethodDesc = lstStudent.OrderByDescending(a => a.Age).ToList();

GroupBy – group by clause is used to get grouped record on the basis of keys
By Query Syntax
var groupby = (from a in lstStudent group a by a.Name).ToList();

By Method Syntax
var groupbyMethod = (lstStudent.GroupBy(a=>a.Name)).ToList();  

Join – join operator is used to get unique values from both lists on key basis 
By Query Syntax
var joina = (from a in lstStudent
                         join
                             m in lstCustomer
                             on a.Name equals m.Name
                         select new {
                         a.Name,
                         a.Age
                         }).ToList();

By Method Syntax
var joinViaMethod = lstStudent.Join( // OuterSequence
                                lstCustomer,     //Inner Sequence
                                student => student.Name, //OuterKey
                                customer => customer.Name,//InnerKey
                                (student, customer) => new {
                                Name= student.Name,
                                age= student.Age
                                }).ToList();


Sum -- To get Sum value for Numeric Item
 By Query Syntax

var sum = (from a in lstStudent
                       select a.Age).Sum();

By Method Syntax
var sumMethod = lstStudent.Sum(a=>a.Age);

Max -To get Maximum value for Numeric Item
By Query Syntax
var max= (from a in lstStudent
                       select a.Age).Max();

By Method Syntax
var maxMethod = lstStudent.Max(a => a.Age);
Average
By Query Syntax
var avg = (from a in lstStudent
                       select a.Age).Average();

By Method Syntax
var avgMethod = lstStudent.Average(a => a.Age);

Min - To get Minimum value for Numeric Item
By Query Syntax
var min = (from a in lstStudent
                       select a.Age).Min();

By Method Syntax
var minMethod = lstStudent.Min(a => a.Age);


Skip & Take -  Skip() methods skips the number of elements from first element and returns the rest
  Take() methods returns the number of elements specified

By Query Syntax
var skip = lstStudent.Skip(2).ToList();
          var take = lstStudent.Take(2).ToList();

var ThirdHighestSal = (from a in lstStudent
                                    orderby a.Age descending
                                    select a.Age).Take(3).Skip(2);


let – let operator is used as a intermediate to get som calculation or get data on function basis
By Query Syntax

var withLetKeywork = from query in lstStudent
                                 let NameinLower = query.Name.ToLower()
                                 where NameinLower.StartsWith("a")
                                 select NameinLower;

By Method Syntax

Into – It is used to continue query after select statement as well as group by statement
By Query Syntax

            var groupByKeywork = from gp in lstStudent
                                 select gp into studentLower
                                 where studentLower.Name.Contains("A1")
                                 select studentLower;




Companies Question/Answers Set -1

1.     Basics pillars of OOPs? 2.     What are the use of new keyword in overriding? 3.     What is the difference between abstra...

Powered by Blogger.