Friday, April 25, 2014

using First, FirstOrDefault, DefaultIfEmpty, Single, SingleOrDefault

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay", "Maya" };
int[] number = { 1, 5, 6, 8, 20, 15, 34, 67, 98, 12, 23 };
 var num = number.Where(n => n > 100).FirstOrDefault(); //num=0
 var numm = number.Where(n => n > 100).First(); //will give an error
 var nummm = number.Where(n => n > 100).DefaultIfEmpty(1).First(); //nummm=1
 var nam = names.Where(n => n.StartsWith("z")).FirstOrDefault();//nam=null
 var name = names.Where(n => n.StartsWith("M")).SingleOrDefault();
//will give an error because two values found with “M”

No comments :

Post a Comment

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath