Question 1:What is Language Integrated Query (LINQ)?
Answer : LINQ is a set of extensions to .NET Framework that encapsulate language integrated query, set and other transformation operations. It extends VB, C# with their language syntax for queries. It also provides class libraries which allow a developer to take advantages of these features.
Question 2:what is difference between LINQ and Stored Procedures?
Answer : 1-Stored procedures normally are faster as they have a predictable execution plan. Therefore, if a stored procedure is being executed for the second time, the database gets the cached execution plan to execute the stored procedure.
2-LINQ supports type safety against stored procedures.
3-LINQ supports abstraction which allows framework to add additional improvements like multi threading. It’s much simpler and easier to add this support through LINQ instead of stored procedures.
4-LINQ allows for debugging using .NET debugger, which is not possible in case of stored procedures.
5-LINQ supports multiple databases against stored procedures which need to be re-written for different databases.
6-Deploying LINQ based solution is much simpler than a set of stored procedures
Question 3:Pros and cons of LINQ (Language-Integrated Query)
Answer :Pros of LINQ:Supports type safety
Supports abstraction and hence allows developers to extend features such as multi threading.
Easier to deploy
Simpler and easier to learn
Allows for debugging through .NET debugger.
Support for multiple databases
Cons of LINQ:
LINQ needs to process the complete query, which might have a performance impact in case of complex queries
LINQ is generic, whereas stored procedures etc can take full advantage of database features.
If there has been a change, the assembly needs to be recompiled and redeployed.
Question 4:Disadvantages of LINQ over Stored Procedures
Answer :LINQ needs to process the complete query, which might have a performance impact in case of complex queries against stored procedures which only need serialize sproc-name and argument data over the network.
LINQ is generic, whereas stored procedures etc can take full advantage of the complete database features.
If there has been a change, the assembly needs to be recompiled and redeployed whereas stored procedures are much simpler to update.
It’s much easier to restrict access to tables in database using stored procedures and ACL’s than through LINQ.
Question 5:Can I use LINQ with databases other than SQL Server? Explain how
Answer: LINQ supports Objects, XML, SQL, Datasets and entities. One can use LINQ with other databases through LINQ to Objects or LINQ to Datasets, where the objects and datasets then take care of database specific operations and LINQ only needs to deal with those objects, not the database operations directly.
Question 6:What is Linq to SQL Deferred Loading?
Answer : (C#) -Deferred Loading is a property of Linq to sql, by default it is set true,
Exam – Let’s have two tables -
Blogs Table (BlogID ,Blog Name,owner) and Post table ( PostID, BlogID, Title, Body)
To find Name and No’s of posts in each blog-
BlogDataContext ctx = new BlogDataContext( );
var query = from b in ctx.Blogs select b;
foreach (Blog b in query)
{
Console.WriteLine("{0} has {1} posts", b.BlogName, b.Posts.Count);
}
Question 7:What is Lambda Expressions? How can we optimize our linq code using this Expression?
Answer :Lambda expressions can be considered as a functional superset of anonymous
methods, providing the following additional functionality:
Lambda expressions can infer parameter types, allowing you to omit them.
Lambda expressions can use both statement blocks and expressions as bodies,
allowing for a terser syntax than anonymous methods, whose bodies can
only be statement blocks.
Lambda expressions can participate in type argument inference and
method overload resolution when passed in as arguments. Note: anonymous
methods can also participate in type argument inference (inferred return types).
In C#, a lambda expression is written as a parameter list, followed by the => token,followed by an expression or a statement block
Question 8:Differentiate between Conversion Operator “IEnumerable” and “ToDictionary” of linq.
Answer: IEnumerable and To Dictionary both are Conversion Operator which are used to solved to conversion type Problems.
“AsEnumerable ” operator simply returns the source sequence as an object of type IEnumerable<T>. This kind of “conversion on the fly” makes it possible to call the general-purpose extension methods over source, even if its type has specific implementations of them
Signature-
public static IEnumerable<T> AsEnumerable<T>
(
this IEnumerable<T> source
);
“ToDictionary ” Conversion Operator is the instance of Dictionary (k,T) . The “keySelector ”predicate identifies the key of each item while “elementSelector ”, if provided, is used to extract each single item.
Key and elementSelector Predicate can be Used in following ways-
Example-
Public void ToDictionatyExample()
{
Var scoreRecords=
{
new {Name = "Alice",Score = 50 },
new {Name = "Bob",Score = 40 },
new {Name = "Cathy", Score = 45} };
Var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name);
Console.WriteLine("Bob's score: {0}", scoreRecordsDict("Bob"));
}
Result: Bob's score: { Name = Bob, Score = 40 }
Answer : LINQ is a set of extensions to .NET Framework that encapsulate language integrated query, set and other transformation operations. It extends VB, C# with their language syntax for queries. It also provides class libraries which allow a developer to take advantages of these features.
Question 2:what is difference between LINQ and Stored Procedures?
Answer : 1-Stored procedures normally are faster as they have a predictable execution plan. Therefore, if a stored procedure is being executed for the second time, the database gets the cached execution plan to execute the stored procedure.
2-LINQ supports type safety against stored procedures.
3-LINQ supports abstraction which allows framework to add additional improvements like multi threading. It’s much simpler and easier to add this support through LINQ instead of stored procedures.
4-LINQ allows for debugging using .NET debugger, which is not possible in case of stored procedures.
5-LINQ supports multiple databases against stored procedures which need to be re-written for different databases.
6-Deploying LINQ based solution is much simpler than a set of stored procedures
Question 3:Pros and cons of LINQ (Language-Integrated Query)
Answer :Pros of LINQ:Supports type safety
Supports abstraction and hence allows developers to extend features such as multi threading.
Easier to deploy
Simpler and easier to learn
Allows for debugging through .NET debugger.
Support for multiple databases
Cons of LINQ:
LINQ needs to process the complete query, which might have a performance impact in case of complex queries
LINQ is generic, whereas stored procedures etc can take full advantage of database features.
If there has been a change, the assembly needs to be recompiled and redeployed.
Question 4:Disadvantages of LINQ over Stored Procedures
Answer :LINQ needs to process the complete query, which might have a performance impact in case of complex queries against stored procedures which only need serialize sproc-name and argument data over the network.
LINQ is generic, whereas stored procedures etc can take full advantage of the complete database features.
If there has been a change, the assembly needs to be recompiled and redeployed whereas stored procedures are much simpler to update.
It’s much easier to restrict access to tables in database using stored procedures and ACL’s than through LINQ.
Question 5:Can I use LINQ with databases other than SQL Server? Explain how
Answer: LINQ supports Objects, XML, SQL, Datasets and entities. One can use LINQ with other databases through LINQ to Objects or LINQ to Datasets, where the objects and datasets then take care of database specific operations and LINQ only needs to deal with those objects, not the database operations directly.
Question 6:What is Linq to SQL Deferred Loading?
Answer : (C#) -Deferred Loading is a property of Linq to sql, by default it is set true,
Exam – Let’s have two tables -
Blogs Table (BlogID ,Blog Name,owner) and Post table ( PostID, BlogID, Title, Body)
To find Name and No’s of posts in each blog-
BlogDataContext ctx = new BlogDataContext( );
var query = from b in ctx.Blogs select b;
foreach (Blog b in query)
{
Console.WriteLine("{0} has {1} posts", b.BlogName, b.Posts.Count);
}
Question 7:What is Lambda Expressions? How can we optimize our linq code using this Expression?
Answer :Lambda expressions can be considered as a functional superset of anonymous
methods, providing the following additional functionality:
Lambda expressions can infer parameter types, allowing you to omit them.
Lambda expressions can use both statement blocks and expressions as bodies,
allowing for a terser syntax than anonymous methods, whose bodies can
only be statement blocks.
Lambda expressions can participate in type argument inference and
method overload resolution when passed in as arguments. Note: anonymous
methods can also participate in type argument inference (inferred return types).
In C#, a lambda expression is written as a parameter list, followed by the => token,followed by an expression or a statement block
Question 8:Differentiate between Conversion Operator “IEnumerable” and “ToDictionary” of linq.
Answer: IEnumerable and To Dictionary both are Conversion Operator which are used to solved to conversion type Problems.
“AsEnumerable ” operator simply returns the source sequence as an object of type IEnumerable<T>. This kind of “conversion on the fly” makes it possible to call the general-purpose extension methods over source, even if its type has specific implementations of them
Signature-
public static IEnumerable<T> AsEnumerable<T>
(
this IEnumerable<T> source
);
“ToDictionary ” Conversion Operator is the instance of Dictionary (k,T) . The “keySelector ”predicate identifies the key of each item while “elementSelector ”, if provided, is used to extract each single item.
Key and elementSelector Predicate can be Used in following ways-
Example-
Public void ToDictionatyExample()
{
Var scoreRecords=
{
new {Name = "Alice",Score = 50 },
new {Name = "Bob",Score = 40 },
new {Name = "Cathy", Score = 45} };
Var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name);
Console.WriteLine("Bob's score: {0}", scoreRecordsDict("Bob"));
}
Result: Bob's score: { Name = Bob, Score = 40 }
No comments:
Post a Comment