41)What is Single operator in LINQ?
Single operator is used to select single or unique or only one element source sequence then we use this operator.
If Sequence contains more than one matching element or no elements then it throws an exception.
42)What is SingleOrDefault operator in LINQ?
It is also working like a Single operator, difference is not throwing any exception if element not exist or more than element exist. it returns defualt vaule in these cases.
43)What is ElementAt and ElementAtOrDefault operators in LINQ?
ElementAt:
Wants to retrive an element form a sequence based on index, use the ElementAt operator , If element not present its throwing an exception.
Index always starting from zero
ElementAtOrDefault:
Same like ElementAt operator , rather than throwing an exception it is return default value.
44)What is DefaultIfEmpty operator in LINQ?
If a sequence is empty it returns default value, if not its return elements in the sequence.
this method is very useful while unit testing the code so that you can replace null with default value.
45)What is Concat operator in C#.NET?
Concat operator appends one sequence to other sequence. For both sequences data source should be same.
46)What is SequenceEqual operator in LINQ?
It is used compare two sequences. If both are equal ,it is return true.(getting issue with reference types comparison)
47)What is AsEnumerable operator in LINQ?
AsEnumerable operator is used to convert source sequence into enumerable object.So that we can apply all the extension methods.
var result =from s in studentsList.AsEnumerable()
where s.City == "Hyderabad"
select s;
48)What is ToArray and ToList operators in LINQ?
Used to convert a source sequence into of type ToArray or ToList.
49)What is difference between Take(1) , First() operator in LINQ?
Both operators return first element in the sequence.
Take(1) not through any exception if element not found where as First operator through an exception.
50)What is Lookup and Dictionary operator in LINQ?
Lookup:
Lookup is immutable: it has no Add() methods.
It is organizing the data in the from key value pair
you can query a non-existent key without an exception.
you have a sequence of data and just want a read-only view
Dictionary:
Dictionary is mutable.
It is organizing the data in the from key value pair.
If you want to map of key to multiple values that is constantly being modified, then a Dictionary<Key, List<Value>> is probably better.
if you want to get an exception if key not found then use dictionary
51)What is OfType and Cast operators in LINQ?
Both operators are used to cast the result into the specific type.
If result not supporting also OfType not returning any exception but Cast operator returning as exception - an InvalidCastException.
52)Can we call parametrize query from LINQ?
Yes , you can call , that statement would be the final statement.
var queryPConstructor =
from c in Customers
where c.Country == "INDIA"
orderby c.CompanyName
select new CustomerData( c.CustomerID, c.CompanyName.ToUpper());
53)How the query execution is done in LINQ to SQL?
Every LINQ to SQL query is initially represented in memory as an expression tree.
The LINQ to SQL engine converts this tree into an equivalent SQL query, traverse the tree and generating the corresponding code.
54)What is LINQ to SQL?
-LINQ to SQL used to query data.
-Using LINQ to SQL you can query a relational structure stored in a SQL Server database
-This is handling entity classes that mapped to database with help of attributes
-All stored procedures and functions handled as methods of a class
55) What is Expression tree?
Expression tree is data structure that represent expression using nodes as operands and operators.
56)How to write LINQ queries In easy way?
In first step specify DataSource means from clause comes first to introduce the DataSource and range variable.
public class Student
{
public int Sno { get; set; }
public string FirstName { get; set; }
public string LastnName { get; set; }
public string City { get; set; }
public List<int> Marks { get; set; }
static void Main()
{
List<Student> StudentObj = new List<Student>();
StudentObj.Add (new Student { Sno= 1, FirstName="Gummadidala", LastnName="Shiva", City = "Hyderabad", Marks = new List<int>{80,80,90}});
StudentObj.Add (new Student { Sno = 2, FirstName = "Abc", LastnName = "Amar", City = "Hyderabad", Marks = new List<int> { 50, 70, 60 } });
StudentObj.Add (new Student { Sno = 3, FirstName = "Ghi", LastnName = "Bhagat", City="Khammam", Marks = new List<int> { 30, 70, 20 } });
StudentObj.Add (new Student { Sno = 4, FirstName = "Mno", LastnName = "Madhu", City="Vijayawada", Marks = new List<int> { 20, 50, 80 } });
IEnumerable<Student> slinq = from stu in StudentObj
where stu.City == "Hyderabad"
select stu;
foreach(var studata in slinq)
{
Console.WriteLine( studata.FirstName );
}
Console.ReadLine();
}
}
57)Write simple program for illustrating Where clause?
IEnumerable<Student> slinq = from stu in StudentObj
where stu.City == "Hyderabad"
select stu;
58)Write simple program for illustrating orderby clause?
IEnumerable<Student> slinq = from cust in StudentObj
orderby cust.City ascending
select cust;
foreach(var studata in slinq)
{
Console.WriteLine(studata.FirstName + " " + studata.LastnName +" is from " + studata.City);
}
o/p:
Gummadidala Shiva is from Hyderabad
Abc Amar is from Hyderabad
Ghi Bhagat is from Khammam
Mno Madhu is from Vijayawada
59)Write simple program for illustrating group clause?
Group clause is used to group the results based on key which was specified.
Here two foreach statements should be used.
Outer foreach iterating over each group.
Inner foreach iterating over each group members.
var slinq = from stu in StudentObj
group stu by stu.Sno;
foreach(var studata in slinq)
{
foreach (Student stu in studata)
{
Console.WriteLine(stu.FirstName + " " + stu.LastnName + " is from " + stu.City);
}
}
60)Write simple program for illustrating join clause?
Join clause is used for joining the two collection objects depend on particular property.
Returning results totally choice of user either return left side object or return right side object.
static void Main()
{
List<Student> StudentObj = new List<Student>();
StudentObj.Add(new Student { Sno= 1, FirstName="Gummadidala",LastnName="Shiva", City = "Hyderabad",Marks = new List<int>{80,80,90}});
StudentObj.Add(new Student { Sno = 2, FirstName = "Abc", LastnName = "Amar", City = "Nalgonda", Marks = new List<int> { 50, 70, 60 } });
StudentObj.Add(new Student { Sno = 3, FirstName = "Ghi", LastnName = "Bhagat", City = "Vijayawada", Marks = new List<int> { 30, 70, 20 } });
StudentObj.Add(new Student { Sno = 4, FirstName = "Mno", LastnName = "Madhu", City = "Hyderabad", Marks = new List<int> { 20, 50, 80 } });
List<StudentGroup> SGObj = new List<StudentGroup>();
SGObj.Add(new StudentGroup { Sno = 1, Gorupname = "MPC"});
SGObj.Add(new StudentGroup { Sno = 5, Gorupname = "Bipc"});
var slinq = from stu in StudentObj
join grp in SGObj on stu.Sno equals grp.Sno
select grp;
foreach(var studata in slinq)
{
Console.WriteLine(studata.Gorupname);
}
Console.ReadLine();
}
O/P:
61) How to remove an element from array in C#.NET using LINQ?
There is no direct method to remove an elements from the list ,using Except we achieve this
int[] a =new int[]{1,2,3,4};
Var res=a.Except(new int[]{2}).ToArray();
Foreach(var item in a)
Console.WriteLine(item);
Output:
1
3
4
MPC
Overview
This classes available in System.Linq.Expressions
Overview
This classes available in System.Linq.Expressions
Language-Integrated Query (LINQ) is introduced in VS2008.
Its provide easy way for retrieving and updating the data.
Its support different kind DataSource like collections, SQL Server databases, ADO.NET Datasets, and XML documents.
In Visual Studio you can write queries with Visual Basic or C#.
Most important thing is LINQ DataSource is any object that supports IEnumerable <T> interface.
LINQ query execution process:
we write LINQ query and we assign it to query variable.
We have two types of executions,
Deferred execution
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class Exercise1
{
public static void Main( )
{
string[] mycolors = { "White", "block", "red", "blue" };
var listColor = new List<string> (mycolors);
var result = from mycolor in listColor
where mycolor.Length >3
select mycolor;
listColor.Remove("White");
System.Console.WriteLine(result.Count());
}
}
output
2
- If not removing white then count is 3
Forcing immediate execution
query variable only store the query commands actual execution happen until the iterate over the query variable with foreach statement this is called deferred execution.
When ever you use aggregate functions like Min, Max, Average..Etc. this will leads to execution of foreach statement implicitly but its only return integer value not IEnumerable collection.
If you use ToList(), ToArray() you can cache all the data in one collection object.
For
Part-1
click here
Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.
No comments:
Post a Comment