RSS

IEnumerable vs IQueriable

IEnumerable

-IEnumerable exists in System.Collections Namespace.
-IEnumerable can move forward only over a collection, it can’t
move backward and between the items.
-IEnumerable is best to query data from in-memory collections like
List, Array etc.
-While query data from database, IEnumerable execute select query
on server side, load data in-memory on client side and then filter
data.
-IEnumerable is suitable for LINQ to Object and LINQ to XML
queries.
-IEnumerable supports deferred execution.
-IEnumerable doesn’t supports custom query.
-IEnumerable doesn’t support lazy loading. Hence not suitable for
paging like scenarios.
-Extension methods supports by IEnumerable takes functional
objects.

IEnumerable Example

MyDataContext dc = new MyDataContext ();
IEnumerable<Employee> list = dc.Employees.Where(p =>
p.Name.StartsWith(“S”));
list = list.Take<Employee>(10);

Generated SQL statements of above query will be :

SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee]
AS [t0]
WHERE [t0].[EmpName] LIKE @p0

Notice that in this query “top 10″ is missing since IEnumerable
filters records on client side

IQueryable

-IQueryable exists in System.Linq Namespace.
-IQueryable can move forward only over a collection, it can’t move
backward and between the items.
-IQueryable is best to query data from out-memory (like remote
database, service) collections.
-While query data from database, IQueryable execute select query
on server side with all filters.
-IQueryable is suitable for LINQ to SQL queries.
-IQueryable supports deferred execution.
-IQueryable supports custom query using CreateQuery and Execute
methods.
-IQueryable support lazy loading. Hence it is suitable for paging
like scenarios.
-Extension methods supports by IQueryable takes expression objects
means expression tree.

IQueryable Example

MyDataContext dc = new MyDataContext ();
IQueryable<Employee> list = dc.Employees.Where(p =>
p.Name.StartsWith(“S”));
list = list.Take<Employee>(10);

Generated SQL statements of above query will be :

SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM
[Employee] AS [t0]
WHERE [t0].[EmpName] LIKE @p0

Notice that in this query “top 10″ is exist since IQueryable
executes query in SQL server with all filters.

 
Leave a comment

Posted by on January 22, 2015 in .NET

 

Extending using Extension methods

if we want the new method to accept some parameters. Well to do this we can define additional parameters after the first parameter that is of the type to be extended (used with this keyword . Let define one more function in int calledMultiply to see this in action.

static class MyExtensionMethods
{

public static int Multiply(this int value, int multiplier)

{
return value * multiplier; // 10*2
}
}

static void Main(string[] args)
{
// Passing arguments in extension methods
int i3 = 10;
Console.WriteLine(“Passing arguments in extension methods: {0}”, i3.Multiply(2));
}

 
Leave a comment

Posted by on January 22, 2015 in .NET

 

Why only one Clustered Index per table?

Clustered index defines the way in which data is
ordered physically on the disk. And there can only be one way in
which you can order the data physically. Hence there can only be
one clustered index per table.

 
Leave a comment

Posted by on January 22, 2015 in .NET

 

Abstract Class

In dynamic polymorphism the object of class reused with polymorphism method which has called only in the runtime. The decision about function execution is made at run time. Method overloading, method overriding, hiding comes under this approach.

The mechanism of linking a function with an object at run time is called dynamic or late binding.

C# uses two approaches to implement dynamic polymorphism,

  1. Abstract Classes
  2. Virtual Function

A class can be consumed from other classes in two different approaches,

  1. Inheritance (Inherit & Consume).
  2. By creating the object and consume.

Abstract classes: – The class under which we declared this abstract method is known as an abstract class and should be declared abstract modifier.

An abstract class can contain abstract members as well as non-abstract members.

A Method without any method body is known as an Abstract method. It contains only the declaration of the method, it should be declared by using the abstract modifier. This incomplete (abstract methods) must be implemented in a derived class.

An abstract class cannot be instantiated directly. An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited. If it is so, it is useless.

An abstract method is implicitly a virtual method. This is accomplished by adding the keyword abstract before the return type of the method. An abstract member cannot be static.

The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.

Use abstract classes when you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes. As it simplifies versioning, this is the practice used by the Microsoft team which developed the Base Class Library. (COM was designed around interfaces.)

So, abstract class defines a common base class for a family of types with a default behavior

For e.g. again take the example of the Vehicle class above. If we want all classes deriving from Vehicle to implement the Drive() method in a fixed way whereas the other methods can be overridden by child classes. In such a scenario we implement the Vehicle class as an abstract class with an implementation of Drive while leave the other methods / properties as abstract so they could be overridden by child classes.

An Abstract class can

  • Had instance variables (like constants and fields), constructors and destructor.
  • Can inherit from another abstract class or another interface.

An Abstract class cannot

  • Inherited by structures.
  • Support multiple inheritances.

 

  1. The concept of abstract method is nearly related with the concept of method overriding. Where in overriding parent class declared a method as virtual and child class re-implements that method by using the override keyword.
  2. In case of abstract method parent class method is abstract which has to be implemented by the child class by using the override keyword only.
  3. The method overriding re-implemented/overriding the method is optional in virtual methods. Where as in abstract methods implementing/overriding the method is mandatory.

abs n vir

 
Leave a comment

Posted by on July 9, 2014 in .NET

 

Difference between Response.Redirect() and Server.Transfer()

Response.Redirect() is used to navigate the user request between multiple webservers
whereas Server.Transfer() is used to navigate the user request within the webserver.

Response.Redirect() will not hide the Destination url address.
Server.Transfer() will hide the destination url address

Viewstate and hiddenfields data is collapsed in both cases of redirect or transfer

If you are using Server.Transfer then you can directly access the values, controls and properties of the previous page which you can’t do with Response.Redirect, Instead you can use querystrings

Server.Transfer sends a request directly to the web server and the web server delivers the response to the browser. So it is faster since there is one less roundtrip. but again it all depends on your requirement.

Response.Redirect can be used for both .aspx and HTML pages whereas Server.Transfer can be used only for .aspx pages and is specific to ASP and ASP.NET.

Both Response.Redirect and Server.Transfer have the same syntax like:
Response.Redirect(“login.aspx”);
Server.Transfer(“login.aspx”);

 
Leave a comment

Posted by on July 4, 2014 in .NET

 

Logic for two tables mismatched columns

create table staging (clientid int primary key,addressdetails varchar(250));
insert into staging
select 100,’hyderbad,india’ union all
select 101,’banglore,india’ union all
select 102,’banglore,india’

create table oltp (client_id int primary key,address_details varchar(250));
insert into oltp
select 104,’newyork,usa’ union all
select 105,’chicago,usa’ union all
select 106,’washington,usa’

select * from oltp where client_id in (select client_id from staging)
o/p:
it returns all 2nd table rows.. instead of raising error that columnname is not existing in table

It will fetch all the values from outer query as inner query referring the same column from outer query .If a column is referenced in a subquery that does not exist in the table referenced by the subquery’s FROM clause, but exists in a table referenced by the outer query’s FROM clause, the query executes without error. SQL Server implicitly qualifies the column in the subquery with the table name in the outer query.

 
Leave a comment

Posted by on July 4, 2014 in .NET

 

With ties clause in sqlserver

SELECT TOP(6) WITH TIES COL

FROM (

SELECT 1 COL UNION ALL

SELECT 2 UNION ALL

SELECT 3 UNION ALL

SELECT 4 UNION ALL

SELECT 5 UNION ALL

SELECT 4 UNION ALL

SELECT 3 UNION ALL

SELECT 4

) A

ORDER BY COL

Answer:

1,2,3,3,4,4,4

Explanation:

Using TOP with “WITH TIES” give all matching values with the last TOP (n) rows in ORDER BY columns. In simple way, if you will specify top 3 then it will give result 1,2,3,3 as there are two same value in that column.
For top 5, it will give the result as 1,2,3,3,4,4,4 because top 5th value is 4. It will find same value in the column used in ORDER BY until the last row of the table.
That’s why top 6 returned 1,2,3,3,4,4,4 as top 6th is 4 so it will look for value 4 in entire COL column values. If matches are found, then it will include all those as well.
Refs: http://msdn.microsoft.com/en-IN/library/ms189463(v=sql.90).aspx
http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/
 
Leave a comment

Posted by on July 4, 2014 in .NET

 
 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: