Quote for the Week

"Learn to enjoy every moment of your life"

Thursday, August 13, 2015

LINQ First() vs FirstOrDefault() and Single() vs SingleOrDefault()

LINQ provides element operators which return a single element or a specific element from a collection. The elements operators are Single, SingleOrDefault, First, FirstOrDefault, Last, LastOrDefault.

Single

It returns a single specific element from a collection of elements if element match found. An exception is thrown, if none or more than one match found for that element in the collection.

SingleOrDefault

It returns a single specific element from a collection of elements if element match found. An exception is thrown, if more than one match found for that element in the collection. A default value is returned, if no match is found for that element in the collection.
List<int> data = new List<int> { 10, 20, 30, 40, 50 };

//Try to get element at specified position
Console.WriteLine(data.ElementAt(1)); //result:20 

//Try to get element at specified position if exist, else returns default value
Console.WriteLine(data.ElementAtOrDefault(10)); //result:0, since default value is 0 

Console.WriteLine(data.First()); //result:10 
Console.WriteLine(data.Last()); //result:50

//try to get first element from matching elements collection
Console.WriteLine(data.First(d => d <= 20)); //result:10 

//try to get first element from matching elements collection else returns default value
Console.WriteLine(data.SingleOrDefault(d => d >= 100)); //result:0, since default value is 0 

//Try to get single element 
// data.Single(); //Exception:Sequence contains more than one element 

//Try to get single element if exist otherwise returns default value
// data.SingleOrDefault(); //Exception:Sequence contains more than one element 

//try to get single element 10 if exist
Console.WriteLine(data.Single(d => d == 10)); //result:10 

//try to get single element 100 if exist otherwise returns default value
Console.WriteLine(data.SingleOrDefault(d => d == 100)); //result:0, since default value is 0

First

- It returns first specific element from a collection of elements if one or more than one match found for that element. An exception is thrown, if no match is found for that element in the collection.

FirstOrDefault

  • It returns first specific element from a collection of elements if one or more than one match found for that element. A default value is returned, if no match is found for that element in the collection.

When to use Single, SingleOrDefault, First and FirstOrDefault ?

  • You should take care of following points while choosing Single, SingleOrDefault, First and FirstOrDefault:
  • When you want an exception to be thrown if the result set contains many records, use Single or SingleOrDefault.
  • When you want a default value is returned if the result set contains no record, use SingleOrDefault.
  • When you always want one record no matter what the result set contains, use First or FirstOrDefault.
  • When you want a default value if the result set contains no record, use FirstOrDefault.

Perfomance of SingleOrDefault and FirstOrDefault

  • FirstOrDefault usually perform faster as compared SingleOrDefault, since these iterate the collection until they find the first match. While SingleOrDefault iterate the whole collection to find one single match.

Friday, July 24, 2015

Microsoft IE8 browser support ending in 17 months

Microsoft is ending support for Internet Explorer 8, announcing it would give users 17 months to stop using the version, which is the most popular version so far.

The post from Microsoft includes a list of operating systems and browser version combinations that would continue getting support, with Internet Explorer 8 not making the cut.

"After Jan. 12, 2016, only the most recent version of Internet Explorer available for a supported operating system will receive technical support and security updates," said Roger Capriotti, director of Internet Explorer, in the full blog post. "For example, customers using Internet Explorer 8, Internet Explorer 9, or Internet Explorer 10 on Windows 7 SP1 should migrate to Internet Explorer 11 to continue receiving security updates and technical support."

Microsoft



The news is especially big for businesses, many of which have not upgraded their systems to Windows 7 or 8 because it means that they would also have to upgrade Internet Explorer.

The post says Microsoft would only be supporting IE9 on Windows Vista, IE10 on Windows Server 2012 and IE11 on Windows 7 and Windows 8.1.

While the browsers will stop getting updates and technical support from Microsoft, they will continue to work on the systems they're installed on.

"Running a modern browser is more important than ever for the fastest, most secure experience on the latest Web sites and services," Capriotti continued in his blog post.

The news takes Microsoft in a different direction from its previous support policy, in which it promised to continue supporting a version of IE as long as an operating system was able to run it.

Under the old policy, IE7 was to continue getting support until 2017, which is when Windows Vista support was to end. IE8 would have continued to get support until 2020, when Windows 7 was to retire. IE10 was supposed to continue getting support until 2023, the end date for Windows 8.

Microsoft has essentially taken off a year of support for IE7, four years for IE8 and IE9 and seven years for IE10.

This news is especially surprising considering the user base and the rate of growth of IE8. The browser is being used by 37 percent of Internet Explorer users, which is a lot more than IE11's 29 percent. Not only that, but in the last month alone IE8 use has grown four times that of IE11.

While at first glance it may seem like Microsoft is losing its mind, the company suggests users will have a better web experience with new versions of Internet Explorer. Not only that, but the move will obviously also help cut Microsoft's support costs. 

LikeFollowShare(167)Tweet(47)Reddit9 Comments

Tuesday, March 31, 2015

How to get file size before upload using jquery

Fille uploading functionality is generally used by the developers. Before uploading file on the server, to know the size of file is a good practice. By knowing file size, we can restrict the end user to upload large size files on the server since we have limited space on the server. We should check uploaded file size on server side as well client side. In this article I am going to expose how you can get size of file before uploading on client side using JQuery. 

Example to get file size before upload using JQuery


 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>Get File Size</title> 
 <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript" > </script>
 <script type="text/javascript">
 function GetFileSize(fileid) {
 try {
 var fileSize = 0;
 //for IE
 if ($.browser.msie) {
 //before making an object of ActiveXObject, 
 //please make sure ActiveX is enabled in your IE browser
 var objFSO = new ActiveXObject("Scripting.FileSystemObject"); var filePath = $("#" + fileid)[0].value;
 var objFile = objFSO.getFile(filePath);
 var fileSize = objFile.size; //size in kb
 fileSize = fileSize / 1048576; //size in mb 
 }
 //for FF, Safari, Opeara and Others
 else {
 fileSize = $("#" + fileid)[0].files[0].size //size in kb
 fileSize = fileSize / 1048576; //size in mb 
 }
 alert("Uploaded File Size is" + fileSize + "MB");
 }
 catch (e) {
 alert("Error is :" + e);
 }
}
 </script>
</head>
<body>
<form name="upload" action="">
<input type="file" name="fUpload" id="fUpload" />
<input type="button" value="Get File Size" onclick="GetFileSize('fUpload');" />
</form>
</body>
</html> 

Tuesday, March 3, 2015

Outlook Integration in .Net

First You have to add Microsoft.Office.Interop.Outlook using References.To do that go to Solution Explorer->Reference->Add Reference->.NET tab->Microsoft.Office.Interop.Outlook.

After that we will create a small class called SendEmails and we will implement a method called sendGroupmail.

Here the Implementation for SendEmails Class.

public class SendEmails
{
public int sendGroupMail(string recipientsList, string subject, string body)
{
Outlook.Application objectOutLook = new Outlook.Application();

try
{
Outlook.MailItem mailItem = objectOutLook.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mailItem.Subject = subject;
mailItem.To = recipientsList;
mailItem.Body = body;
Outlook.NameSpace ns = objectOutLook.GetNamespace("mapi");
ns.Logon(Missing.Value, Missing.Value, true, true);
mailItem.Importance = Outlook.OlImportance.olImportanceLow;
((Outlook.MailItem)mailItem).Send();
ns.Logoff();
return 1; 
}
catch (Exception ex2)
{
MessageBox.Show(ex2.Message);
return 0;
}

}
}

How to use this class.........

SendEmails newemail=new SendEmails();
int result = newemail.sendGroupMail("example@mail.com", "This is Subject", "This is Body");