IntroductionI have found new operators in Linq which is use full in day to day programming stuff. Take,Skip,Union,Except ,Intersect operator and Reverse,. Here are explanation of operators how it works. Take Operator Take operator will return first N number of element from entities. Skip OperatorSkip operator will skip N number of element from entities and then return remaining elements as a result. Reverse OperatorAs name suggest it will reverse order of elements of entities. Sample Codeusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{class Program
{static void Main(string[] args)
{string[] a = { "a", "b", "c", "d" };
Console.WriteLine("Take Example");
var TkResult = a.Take(2);foreach (string s in TkResult)
{
Console.WriteLine(s);
}
Console.WriteLine("Skip Example");
var SkResult = a.Skip(2);foreach (string s in SkResult)
{
Console.WriteLine(s);
}
Console.WriteLine("Reverse Example");
var RvResult = a.Reverse();foreach (string s in RvResult)
{
Console.WriteLine(s);
}
}
}
} OutputUnion OperatorUnion operator will combine elements of both entity and return result as third new entities. Except OperatorExcept operator will remove elements of first entities which elements are there in second entities and will return as third new entities. Intersect OperatorAs name suggest it will return common elements of both entities and return result as new entities. Sampleusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{static void Main(string[] args)
{string[] a = { "a", "b", "c", "d" };string[] b = { "d","e","f","g"};
var UnResult = a.Union(b);
Console.WriteLine("Union Result");foreach (string s in UnResult)
{
Console.WriteLine(s);
}
var ExResult = a.Except(b);
Console.WriteLine("Except Result");foreach (string s in ExResult)
{
Console.WriteLine(s);
}
var InResult = a.Intersect(b);
Console.WriteLine("Intersect Result");foreach (string s in InResult)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
}
in above code a simple console application as a example where i have used two string array and applied the three operator one by one and print the result using Console.Writeline. Here is the code for that. OutputHope this will help you. |