Tuesday, August 16, 2011

Explain Query Expression syntax, Fluent syntax, Mixed Queries.


Query expression syntax is based on the new keywords such as from, select, join, group by, order byetc.
string[] words = { "roll", "removal", "fuse", "accusation", 
                             "capture", "poisoning", "accusation" };

var query = from w in words
            where w.Length > 4
            orderby w ascending
            select w;
This query selects words with more than 4 letters and presents result in the ascending order. Fluent syntax is based on the regular C# methods that are linked in a chain, like this sample from msdn.
List<Customer> customers = GetCustomerList();
 
var customerOrders = customers
	.SelectMany(
		(cust, custIndex) => cust
			.Orders
			.Select(o => 
				"Customer #" + (custIndex + 1) +
				" has an order with OrderID " + o.OrderID));
Mixed syntax means query expression syntax is mixed with fluent method calls, for example it can be used to get the distinct values, first result or to get items as array (which by the way will trigger immediate query execution)

No comments:

Post a Comment