8.23.2012

LINQ Difference FAQs-3

1.Difference between First() and Single() extension methods in LINQ
S.No First() Single()
1
There is at least one result, an exception is thrown if no result is returned.
There is exactly 1 result, no more, no less, an exception is thrown if no result is returned.
2.Difference between FirstOrDefault() and SingleOrDefault() extension method in LINQ
S.No
FirstOrDefault()
SingleOrDefault()
1
It gets the first item that matches a given criteria.
If we specify this extension method that means we are specifically saying that there can be only one value that matches the criteria. If there are more then 1 value that matches the criteria, throw an exception.
3.Difference between Count() and LongCount() extension methods in LINQ
S.No Count() LongCount()
1
Count() has a lesser range than LongCount()

long.MinValue = -2,147,483,648
long.MaxValue = 2,147,483,647
LongCount() has a greater range than Count().

long.MinValue = -9223372036854775808
long.MaxValue = 9223372036854775807
2
Its DotNet Framework type is System.Int32
Its DotNet Framework type is System.Int64

Example for Count():

public static long display()
{
var tempval = (from h in objDB.tbl_mvc_login
select h).Count ();

return tempval;
}

Example for LongCount():

public static long display()
{
var tempval = (from h in objDB.tbl_mvc_login
select h).LongCount ();

return tempval;
}

Summary:

They both does the same thing .If we want to count something which is quite big then we have to use LongCount() extension method otherwise we can use Count().

No comments:

Post a Comment