LINQ : Standard Query Operators
1 > Filtering
Filtering operators define a restriction to the elements returned with the Where query operator you can use a predicate, for example defiend by a Lambda expression that returns a bool OfType<T> filters the elements based on the type and returns only the elements of the type T.
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
IEnumerable<string> query =
names.Where (name => name.EndsWith ("y"));
query = from n in names
where n.EndsWith ("y")
select n;
Using multiple where clauses
var names = new[] { "Tom", "Dick", "Harry", "Mary", "Jay" }.AsQueryable();
var query =
from n in names
where n.Length > 3
let u = n.ToUpper()
where u.EndsWith ("Y")
select u;
2 > Projection
Projection operators are used to transform an object into a new object of a different type. Select and SelectMany define a projection to select values of the result based on a selector function.
1> var query = from f in FontFamily.Families
select f.Name;
2>
using (var Context = new MyEntities())
{
var UDetail = (from u in Context.Users
where u.UserID == UId
select new
{
u.UserName,
u.FirstName,
u.LastName
}).ToList();
if (UDetail.Count > 0)
{
// Some Code Here
}
}
3 > Sorting
Sorting operators change the order of elements returned. OrderBy sorts values in ascending order, OrderByDescending sorts values in descending order. ThenBy and ThenByDescending operators are used for a secondary sort if the first sort gives similar results. Reverse reverses the elements in collection.
using (var Context = new MyEntities())
{
var UserOrderBy = Context.Users.Where(c=>c.UserID==UserID).OrderBy(c=>c.UserID);
var UserOrderByDesc = Context.Users.Where(c=>c.UserID==UserID).OrderByDescending(c=>c.UserID);
var UserThenBy = Context.Users.Where(c=>c.UserID==UserID).OrderBy(c=>c.UserID).ThenBy(c=>c.UserID);
var UserThenByDesc = Context.Users.Where(c=>c.UserID==UserID).OrderByDescending(c=>
c.UserID).ThenByDescending(c=>c.UserID);
}
4 > Join
Join operators are used to combine collections that might not be directly related to each other. With the join operator a join of two collections based on key selector functions can be done. This is similar to the JOIN you know from SQL. The GroupJoin operator joins two collections and groups the results.
using (var Context = new MyEntities())
{
var UDetail = (from r in Context.UserInRoles
join u in Context.Users on r.UserID equals u.UserID
where r.RoleID == RoleID
select new
{
u.UserID,
u.UserName,
u.FirstName,
u.LastName,
r.RoleName
}).ToList();
if (UDetail.Count > 0)
{
/////////
}
}
5 > Grouping
Grouping operators put the data into groups. The GroupBy operator groups elements with a common key.
string[] files = Directory.GetFiles(Path.GetTempPath()).Take(100).ToArray();
files.GroupBy(file => Path.GetExtension(file));
6 > Quantifier
Quantifier operators return a Boolean value if elements of the sequence satisfy a specific condition. Any, All, and Contains are quantifier operators. Any determines if any element in the collection satisfies a predicate function. All determines if all elements in the collection satisfy a predicate. Contains checks whether a specific element is in the collection. These operators return a Boolean value
new int[] { 2, 3, 4 }.Contains (3);

Reeta Singh LodhiPosted Feb 11, 2011, 1:54 AM
Really really a nice blog... quick and brief.
Dinesh BeniwalPosted Feb 11, 2011, 1:53 AM
Useful article mukesh.
Amit ChoudharyPosted Feb 11, 2011, 12:58 AM
Thanks for posting this nice information.. i was looking for such information quick and brief info about operators of LINQ. Keep it up.
Mahesh ChandPosted Feb 10, 2011, 8:28 PM
This is a nice list of various operations. Good work Mukesh!