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}

Reversing integer Array through LINQ in c#

public Int32[] ReverseThroughLINQ(Int32[] _inputs,int k=0)
 {
 if (k==0) {
  k=_inputs.Count();
 }
 int[] reverse = _inputs.Take(k).Reverse().ToArray();
 int[] straight = _inputs.Skip(k).ToArray();
 int[] _concatenatedString = reverse.Concat(straight).ToArray();
 return _concatenatedString;
 }

use Contains in LINQ using C#

var query6 = from n1 in names
 select n1.ToUpper() into n2
 where n1.Contains("a") // Illegal: n1 out of scope.
 select n2;
//rewrite query number 6
 var query10 = from n1 in names
 let n2 = n1.ToUpper()
 where n2.Contains("A")
 select n1;

Ordering generic list IEnumerable

string[] musos = { "Roger Waters", "Rick Wright", "David Gilmour"};
IEnumerable query = musos.OrderBy(m => m.Split().Last());
Using Inner Query

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay", "Maya" };
var outerQuery = names
 .Where(n => n.Length == names.OrderBy(n2 => n2.Length)
 .Select(n2 => n2.Length).First()
 ); //output Tom,Jay

Same query as above

IEnumerable comprehension = from n in names
   where n.Length == (from n2 in names
   orderby n2.Length
   select n;
IEnumerable query4 = names
 .Select(n => Regex.Replace(n, "[aeiou]", ""))
 .Where(n => n.Length > 2)
 .OrderBy(n => n.Length);
IEnumerable query4 = names
 .Select(n => Regex.Replace(n, "[aeiou]", ""))
 .Where(n => n.Length > 2)
 .OrderBy(n => n.Length);

Same as above query

IEnumerable query5 = from n in names
 select Regex.Replace(n, "[aeiou]", "") into g
 where g.Length > 2
 select n2.Length).First()
 orderby g.Length
 select g;

IEnumerable query8 = from n1 in
 (
 from n2 in names
 select Regex.Replace(n2, "[aeiou]", "")
 )
 where n1.Length > 2
 orderby n1
 select n1;

 IEnumerable query9 = from n in names
  let vowelless = Regex.Replace(n, "[aeiou]", "")
  where vowelless.Length > 2
 orderby vowelless
 select n; // Thanks to let, n is still in scope.

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”

Find Customer list whom city exists in city array

String[] _cities = { "London", "New Delhi" };

var _customers = new[] { new { Name = "Saurabh", City = "London" },
 new { Name = "Sumit", City = "New Delhi" },
 new { Name = "Rohit", City = "London" },
 new { Name = "Rahul", City = "Mumbai" },
 new { Name = "Suresh", City = "London" },
 new { Name = "Sunil", City = "New Delhi" },
 new { Name = "Mahesh", City = "Agra" }
 };

var _query = from c in _customers
 where (_cities).Contains(c.City)
 select c;
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