diff --git a/.NET/C#/LINQ .md b/.NET/C#/LINQ .md index fa92d87..e565f9a 100644 --- a/.NET/C#/LINQ .md +++ b/.NET/C#/LINQ .md @@ -10,7 +10,7 @@ LINQ to Objects will be used when any `IEnumerable` is specified as the sourc ### Query Expressions -All query expressions are required to begin with a `from` clause, which specifies the source of the query. +All query expressions are required to begin with a `from` clause, which specifies the source of the query. The final part of the query is a `select` (or `group`) clause. This determines the final output of the query and its sytem type. ```cs @@ -21,11 +21,10 @@ var result = from item in enumerable select item; var result = from item in enumerable where condition select item; // ordering -var result = from item in enumerable orderby item.property select item; // ordered Ienumerble +var result = from item in enumerable orderby item.property select item; // ordered IEnumerble -// let clause -var result = from item in enumerable let tmp = ... // assign expression to variable to avoid re-evaluetion on each cycle -// BEWARE: compiled code has a lot of overhead to satisfy let caluse +// let clause, assign expression to variable to avoid re-evaluetion on each cycle +var result = from item in enumerable let tmp = ... // BEWARE: compiled code has a lot of overhead to satisfy let caluse // grouping (difficult to re-implement to obtain better performance) var result = from item in enumerable group item by item.property; // returns IEnumerable> @@ -33,7 +32,7 @@ var result = from item in enumerable group item by item.property; // returns IE ### How Query Expressions Expand -The compiler converts all query expressions into one or more method calls. Once it has done that, the LINQ provider is selected through exactly the same mechanisms that C# uses for any other method call. +The compiler converts all query expressions into one or more method calls. Once it has done that, the LINQ provider is selected through exactly the same mechanisms that C# uses for any other method call. The compiler does not have any built-in concept of what constitutes a LINQ provider. ```cs @@ -48,24 +47,24 @@ The `Where` and `Select` methods are examples of LINQ operators. A LINQ operator ```cs Enumerable.Range(int start, int end); // IEnumerable of values between start & end -// max item in the IEnumerable -IEnumerable.Max(); // source must implement IComparable +IEnumerable.Select(Func selector) // map +IEnumerable.Where(Func predicate) // filter -// check if condition is true for all IEnumerbale -IEnumerable.All(IEnumerable source, Func predicate); -IEnumerable.All(IEnumerable source.Predicate); +IEnumerable.FirstOrDefault() // first element of IEnumerable or default(T) if empty +IEnumerable.FirstOrDefault(Func predicate) // first element to match predicate or default(T) + +// T must implement IComparable +IEnumerable.Max(); +IEnumerable.Min(); + +IEnumerable.All(Func predicate); // check if condition is true for all elements +IEnumerable.Any(Func predicate); // check if condition is true for at least one element ``` -## LINQ to JSON (JSON.NET) - -Parses JSON data into objects of type `JObject`, `JArray`, `JProperty`, and `JValue`, all of which derivefrom a `JToken` base class. -Using these types is similar to working with JSON from JavaScript: it's possible to access the content directly without having to define classes. +**NOTE**: `Enumerable` provides a set of `static` methods for querying objects that implement `IEnumerable`. Most methods are extensions of `IEnumerable` ```cs -var jo = (JObject) JToken.Parse(json); // parse json - -var propery = jo["property"].Value(); // extract and convert data from json - -// linq query -IENumerable props = jo.Descendants().OfType().Where(p => condition); +Enumerable.Method(IEnumerable source, args); +// if extension method same as +IEnumerable.Method(args) ```