Thursday 18 June 2015

Difference between icollection vs ienumerable vs list

Difference among  IEnumerable<T> , IQueryable<T>, ICollection<T>,IList<T>, List<T>:

IENUMERABLE<T>:

Pros
1.       The most generic item of all.
2.       Still might use deferred execution.
3.       IEnumerable is best suitable for working with queries later on. It helps best when we try to add more queries with our previous one and we do not want to run our previous query in realtime but when we requested it or iterated through those or finally passing as a .ToList() object .
4.       IEnumerable does not run query until it is requested by iteration.

Cons

1.       IEnumerable doesn’t move between items, it is forward only collection as LinkedList. You can't get at "item 4" without passing items 0-3.
2.       Read-only list, you can't add to it or remove from it.
3.       Actually the model binder is not able to bind an enumerable because it is a too generic Interface, and it is not able to choose a concrete class to create that implements the IEnumerable.

IQUERYABLE<T>:

Pros
1.       Query isn't executed until to really iterate over the items, maybe by doing a .ToList()
2.       IQueryable best suits for remote data source, like a database or web service. IQueryable is a very powerful feature that enables a variety of interesting deferred execution scenarios (like paging and composition based queries).

Cons

1.       avoid passing IQueryable to Views
2.       It is not a good place to handle errors...so better getting rid of delayed execution before passing to Views.

ICOLLECTION<T>:
Is between IEnumerable and IList. In addition, is the base of IList and IDictionary Objects.

Pros
1.       Considered the most basic type for collections.
2.       The System.Xml.Serialization.XmlSerializer class has specific requirements for types that implement ICollection and System.Collections.IEnumerable in order to be serializable.
3.      Inherited from IEnumerable<T>.
4.      Collection<T> , IList<T>, IDictionary all are inherited from this, so we have flexibility.
5.      Base of IList and IDictionary Object.
6.      Add, Delete and modify items in the collection is acceptable.
7.       Some collections that limit access to their elements, like the Queue class and the Stack class, directly implement the ICollection interface.
8.       Normally in EF table relationships we use this ICollection in virtual keyword.

Cons
1.       ICollection doesn’t have indexing like IList does.

ILIST<T>:

Pros
1.       Random access to the full list(like an ArrayList)
2.       Entirely in memory
3.      Best for model binding.
4.      Supports adding and removing but for only variable-size one.
5.       An IList implementation is a collection of values that can be sorted and whose members can be accessed by index, like the ArrayList class.
6.       In addition, IList implementations fall into three categories:
a)       Read-only: (A read-only IList cannot be modified)
b)      Fixed-size: (A fixed-size IList does not allow the addition or removal of elements, but it allows the modification of existing elements.)
c)       Variable-size: (A variable-size IList allows the addition, removal and modification of elements.)


Cons

1.       Memory Consuming.

LIST<T>:
Propertise
1.      Implements : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
Pros

1.      The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface using an array whose size is dynamically increased as required.
2.      In deciding whether to use the List<T> or ArrayList class, both of which have similar functionality, remember that the List<T> class performs better in most cases and is type safe.
3.       Methods such as BinarySearch and Sort use an ordering comparer for the list elements. The default comparer for type T is determined as follows. If type T implements the IComparable<T> generic interface, then the default comparer is the CompareTo(T) method of that interface; otherwise, if type T implements the nongeneric IComparable interface, then the default comparer is the CompareTo(Object) method of that interface. If type T implements neither interface, then there is no default comparer, and a comparer or comparison delegate must be provided explicitly.

Cons

1.      List<T> accepts null as a valid value for reference types and allows duplicate elements.
2.       The List<T> is not guaranteed to be sorted. You must sort the List<T> before performing operations (such as BinarySearch) that require the List<T> to be sorted.


In Simple words ;)

IEnumerable: Provides Enumerator for accessing collection
  • Used where you want to store a collection of objects which will be accessed only for read-only purpose.
  • You need to iterate through the collection, means to access an element at position 5 you first need to access 0-4 objects.
  • Cannot modify the list i.e. add, remove object operations not allowed.
ICollection:
  • List can be modified and iterated i.e. read, add, delete, edit operation allowed.
  • Operations like Sort are not allowed
IQueryable:
  • a special type because it allows deferred query execution i.e. if you define any query over IQueryable collection then it won’t execute till the timeGetEnumerator() is called.
  • It is particularly used for Linq queries and in Entity Framework.
  • All other collection types brings data from the database to the client side and then apply filter or do operation on that data. However, IQueryable filters data at database level i.e. filtered data is received from database.
  • Specific example, In Entity Framework based application, you use IQueryable collection for seed data and if you call collection.SaveChanges() then database update command is not sent directly in fact when you will first try to access the data (or first time you call GetEnumerator on DBContext) then database update commands will be sent by entity framework. Good example of deferred query execution.
IList:
  • Used where you need to iterate (read), modify and sort, order a collection
  • Random element access allowed i.e. you can directly access an element at index 5 instead of first iterating through 0-4 elements.

Wednesday 10 June 2015

How to get all options of a select using jQuery?

here is an example code!
$("#ID option").each(function () {
                $(this).prop('selected', true);
            });

ID=> tour item id.

Monday 8 June 2015

first and single in linq difference


refer this below table.

Single()SingleOrDefault()First()FirstOrDefault()
DescriptionReturns a single, specific element of a sequenceReturns a single, specific element of a sequence, or a default value if that element is not foundReturns the first element of a sequenceReturns the first element of a sequence, or a default value if no element is found
Exception thrown whenThere are 0 or more than 1 elements in the resultThere is more than one element in the resultThere are no elements in the resultOnly if the source is null (they all do this)
When to useIf exactly 1 element is expected; not 0 or more than 1When 0 or 1 elements are expectedWhen more than 1 element is expected and you want only the firstWhen more than 1 element is expected and you want only the first. Also it is ok for the result to be empty
for more details,

Monday 1 June 2015

difference between viewbag and viewdata and tempdata in mvc3

the main difference below,
ViewData:
  • ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys
  • It’s required type casting for complex data type and check for null values to avoid error.

ViewBag: 
  • ViewBag doesn’t require typecasting for complex data type.
  • ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0. 

Check the following example:
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var emp = new Employee
        {
            EmpID=101,
            Name = "Deepak",
            Salary = 35000,
            Address = "Delhi"
        };

        ViewData["emp"] = emp;
        ViewBag.Employee = emp;

        return View(); 
    }
}
And the code for View is as follows:
@model MyProject.Models.EmpModel;
@{ 
 Layout = "~/Views/Shared/_Layout.cshtml"; 
 ViewBag.Title = "Welcome to Home Page";
 var viewDataEmployee = ViewData["emp"] as Employee; //need type casting
}
<h2>Welcome to Home Page</h2>
This Year Best Employee is!
<h4>@ViewBag.emp.Name</h4>
<h3>@ViewData.Employee.Name</h3>

Wednesday 27 May 2015

javascript == and === difference

1. === operater
=== means value must be equal and type as well
Ex:
var value1=1;
var value2='1';

if(value1===value2) /// this will return false because this value type is different(javascript will do internal type casting even though if we use === it will follow the strict rules),

and

if(0===false) // this will return false because different data type


2.== operator

== means value must be equal,

Ex:
var value1=1;
var value2='1';
if(value1==value2) /// this will return true because (==) will check only value(javascript handle type conversion).

and
if(0==false) // this will return true because false is equal to 0

Monday 11 May 2015

Best 100+ Frequently Asked Interview Questions in .Net SQL

refer this link

http://www.webblogsforyou.com/100-frequently-asked-interview-questions-on-asp-net-sql-server-oop-concepts/

Thursday 30 April 2015

cannot open backup device operating system error 5(access is denied.)

possible reason for this errors are,

1. there is no directory with the name
ex, if u r mention path as 'D:\test\abc.bak' may be this directory wont be there or not access for your credentials.
2.second possible is, u wont be having enough space in the disk to save the data.
3.you may not having enough permission/rights in logs drive
ex, if your mention a folder name, for that folder u might not having write permission, something like that.

how to take database backup using sql query

in query window use this syntex:

backup datsbase databasename to disk='location to store'


Example:

backup database sureshdatabase in disk = 'D:\sureshDBBackup\sureshdatabase.bak'

after forming this query execute.




Friday 17 April 2015

assign null value to enum in c#

You can use the "?" operator for a enum nullable type.

Ex:
public enumname? objenum = null;

or else u can create one more enum as none then use it.

Friday 27 March 2015

[SOLVED] how to know if page is refreshed in javascript

add this to the need to check is closed or refreshed !!


<script type="text/javascript">

window.onunload = function (e) {
// Firefox || IE
e = e || window.event;
var y = e.pageY || e.clientY;
if (y < 0) {
alert("close");
}
else {
alert("refresh");
}
}

</script>

Monday 2 March 2015

[Solved] http error 503. the service is unavailable iis

Follow the steps:

Steps 1: Open IIS Manager (short command: inetmgr) with Administrator Authentication

Solving IIS 7 Error 503 Service Unavailable
Step 2: Open the Application Pools node underneath the machine node. Select the Application Pool you want to change to run under an automatically generated Application Pool Identity. Here I’m selecting ‘Your Application Name’ Application Pool.

Solving IIS 7 Error 503 Service Unavailable

Step 3: Now right click on the selected Application Pool and click on ‘Advance Settings’ option.

Solving IIS 7 Error 503 Service Unavailable
Solving IIS 7 Error 503 Service Unavailable

Now here you have to set Start Automatically property ‘true’ and set Identity property is ‘ApplicationPoolIdentity’.
Solving IIS 7 Error 503 Service Unavailable
Solving IIS 7 Error 503 Service Unavailable
Now click button ‘Ok’, your configuration setting will be saved.


Reference from : http://www.mindstick.com