Introduction
LINQ is an acronym which stands for Language INtegrated Query. It is a concept which integrates a query language by offering a consistent model for working with data across various kinds of data sources and formats; you use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, .NET collections, and any other format for which a LINQ provider is available.
Syntax
-
Query syntax :
- from <range variable> in <collection>
- [from <range variable> in <collection>, ...]
- <filter, joining, grouping, aggregate operators, ...> <lambda expression>
- <select or groupBy operator> <formulate the result>
-
Method syntax :
- Enumerable.Aggregate(func)
- Enumerable.Aggregate(seed, func)
- Enumerable.Aggregate(seed, func, resultSelector)
- Enumerable.All(predicate)
- Enumerable.Any()
- Enumerable.Any(predicate)
- Enumerable.AsEnumerable()
- Enumerable.Average()
- Enumerable.Average(selector)
- Enumerable.Cast<Result>()
- Enumerable.Concat(second)
- Enumerable.Contains(value)
- Enumerable.Contains(value, comparer)
- Enumerable.Count()
- Enumerable.Count(predicate)
- Enumerable.DefaultIfEmpty()
- Enumerable.DefaultIfEmpty(defaultValue)
- Enumerable.Distinct()
- Enumerable.Distinct(comparer)
- Enumerable.ElementAt(index)
- Enumerable.ElementAtOrDefault(index)
- Enumerable.Empty()
- Enumerable.Except(second)
- Enumerable.Except(second, comparer)
- Enumerable.First()
- Enumerable.First(predicate)
- Enumerable.FirstOrDefault()
- Enumerable.FirstOrDefault(predicate)
- Enumerable.GroupBy(keySelector)
- Enumerable.GroupBy(keySelector, resultSelector)
- Enumerable.GroupBy(keySelector, elementSelector)
- Enumerable.GroupBy(keySelector, comparer)
- Enumerable.GroupBy(keySelector, resultSelector, comparer)
- Enumerable.GroupBy(keySelector, elementSelector, resultSelector)
- Enumerable.GroupBy(keySelector, elementSelector, comparer)
- Enumerable.GroupBy(keySelector, elementSelector, resultSelector, comparer)
- Enumerable.Intersect(second)
- Enumerable.Intersect(second, comparer)
- Enumerable.Join(inner, outerKeySelector, innerKeySelector, resultSelector)
- Enumerable.Join(inner, outerKeySelector, innerKeySelector, resultSelector, comparer)
- Enumerable.Last()
- Enumerable.Last(predicate)
- Enumerable.LastOrDefault()
- Enumerable.LastOrDefault(predicate)
- Enumerable.LongCount()
- Enumerable.LongCount(predicate)
- Enumerable.Max()
- Enumerable.Max(selector)
- Enumerable.Min()
- Enumerable.Min(selector)
- Enumerable.OfType<TResult>()
- Enumerable.OrderBy(keySelector)
- Enumerable.OrderBy(keySelector, comparer)
- Enumerable.OrderByDescending(keySelector)
- Enumerable.OrderByDescending(keySelector, comparer)
- Enumerable.Range(start, count)
- Enumerable.Repeat(element, count)
- Enumerable.Reverse()
- Enumerable.Select(selector)
- Enumerable.SelectMany(selector)
- Enumerable.SelectMany(collectionSelector, resultSelector)
- Enumerable.SequenceEqual(second)
- Enumerable.SequenceEqual(second, comparer)
- Enumerable.Single()
- Enumerable.Single(predicate)
- Enumerable.SingleOrDefault()
- Enumerable.SingleOrDefault(predicate)
- Enumerable.Skip(count)
- Enumerable.SkipWhile(predicate)
- Enumerable.Sum()
- Enumerable.Sum(selector)
- Enumerable.Take(count)
- Enumerable.TakeWhile(predicate)
- orderedEnumerable.ThenBy(keySelector)
- orderedEnumerable.ThenBy(keySelector, comparer)
- orderedEnumerable.ThenByDescending(keySelector)
- orderedEnumerable.ThenByDescending(keySelector, comparer)
- Enumerable.ToArray()
- Enumerable.ToDictionary(keySelector)
- Enumerable.ToDictionary(keySelector, elementSelector)
- Enumerable.ToDictionary(keySelector, comparer)
- Enumerable.ToDictionary(keySelector, elementSelector, comparer)
- Enumerable.ToList()
- Enumerable.ToLookup(keySelector)
- Enumerable.ToLookup(keySelector, elementSelector)
- Enumerable.ToLookup(keySelector, comparer)
- Enumerable.ToLookup(keySelector, elementSelector, comparer)
- Enumerable.Union(second)
- Enumerable.Union(second, comparer)
- Enumerable.Where(predicate)
- Enumerable.Zip(second, resultSelector)
To use LINQ queries you need to import System.Linq
.
The Method Syntax is more powerful and flexible, but the Query Syntax may be simpler and more familiar. All queries written in Query syntax are translated into the functional syntax by the compiler, so performance is the same.
Query objects are not evaluated until they are used, so they can be changed or added to without a performance penalty.