Saturday, April 26, 2014

Using Predicate and Func in linq using c#

//Creation of Predicate

Predicate<String> checkGreterthen5= x=> x.Length>5;

List<String> oLst=new List<string>();

oLst.Add("sa");

oLst.Add("saurabh");

String str = oLst.Find(checkGreterthen5);

Console.WriteLine(str);//Saurabh

//Creation of Func

delegate Double CalcAreaPointer(int r);

CalcAreaPointer _obj= r => 3.14 * r*r;

Double Area=_obj(5);

Func<double,double,double> _obj2=(r,e)=> 3.14 *r*e;

//Action<>

Double Area2 = _obj2(5,2);

Most Occurred Number in Integer Array using Linq using C#

String[] _numbers = { "2", "1", "3", "2", "4", "6", "2", "1", "4", "6", "6", "4", "8", "1", "3", "8" };
var _query2 = from n in _numbers
 group n by n into g
 select new
 {
 number = g.Key,
 count = g.Count()
 };
 var _MaxCount = _query2.Max(a => a.count);
 var mostOccurednumber = _query2
 .Where(g => g.count == _MaxCount)
 .Select(g => g.number);

Friday, April 25, 2014

Merging three array and take top 5 numbers using LINQ in C#

int[] _inputs1 = { 11,12, 13, 14, 15, 16, 17, 18, 169 };
int[] _inputs2 = { 11, 22, 33, 24, 25, 56, 67, 88, 99 };
int[] _inputs3 = { 1, 2, 3, 4, 5, 6, 7, 68, 98 };
Int32[] Concatenate=Input1.Concat(Input2).Concat(Input3).OrderByDescending(a=>a)
 .Take(5).ToArray();
foreach(int a in Concatenate)
{
 Console.writeline(a);
}

//output {168,99,98,88,68}

ShareThis

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