Write Skip, Take, Distinct, ToList, ToHashSet, ToDictionary, etc. fluently in LINQ statement without extra parentheses.
Instead of writing this:
var paged = (from e in employees
where e.JoinDate.Year >= 2018
orderby e.Name ascending
select e).Skip(page * pageSize)
.Take(pageSize)
.ToList();
you can write it without parentheses like this:
var paged = from e in employees
where e.JoinDate.Year >= 2018
orderby e.Name ascending
where Skip(page * pageSize)
where Take(pageSize)
group e by ToList into g
select g;
From Package Manager Console
Install-Package Parenthless
From .NET CLI
dotnet add package Parenthless
Using PackageReference
<PackageReference Include="Parenthless" Version="1.0.3" />
Add using directive
using Parenthless;
using static Parenthless.Linq; // important alias to make all the clauses work
Filters the elements of the sequence based on a specified type.
var filtered = from i in items
where OfType<int>()
select i;
Excludes first n elements from sequence.
var skipped = from i in items
where Skip(3)
select i;
Includes only up to n first elements from sequence.
var taken = from i in items
where Take(10)
select i;
Excludes last n elements from sequence.
var skipped = from i in items
where SkipLast(3)
select i;
Includes only up to n last elements from sequence.
var taken = from i in items
where TakeLast(10)
select i;
Excludes elements from sequence as long as condition is true.
var trimmed = from c in str
where SkipWhile(char.IsWhiteSpace(c))
select c;
Includes elements from sequence as long as condition is true.
var word = from c in str
where SkipWhile(char.IsWhiteSpace(c))
where TakeWhile(char.IsLetterOrDigit(c))
select c;
Fills sequence with one default value if sequence is empty.
var neverEmpty = from i in items
where DefaultIfEmpty
select i;
Concatenates second sequence to the end of current sequence.
var concat = from i in items
where Concat(moreItems)
select i;
Produces set union of current sequence and second sequence.
var union = from i in items
where Union(moreItems)
select i;
Produces set difference of current sequence and second sequence.
var difference = from i in items
where Except(excludedItems)
select i;
Produces set intersection of current sequence and second sequence.
var commonItems = from i in items
where Intersect(otherSequence)
select i;
Removes duplicate elements from sequence.
var unique = from i in items
orderby Distinct
select i;
Reverses sequence.
var lastThree = from i in items
orderby Reverse
where take(3)
select i;
Automatically converts return value to List<T>.
var list = from i in items
group i by ToList into g
select g; // returns a List<T>
Automatically converts return value to T[].
var array = from i in items
group i by ToArray into g
select g; // returns a T[]
Automatically converts return value to HashSet<T>.
var set = from i in items
orderby distinct
group i by ToHashSet into g
select g; // returns a HashSet<T>
Automatically converts return value to Dictionary<TKey, TValue>.
var ageById = from person in people
group person.Age by ToDictionary(person.Id) into dict
select dict;
Automatically converts return value to first element in sequence. Throws exception if sequence is empty.
var first = from i in items
group i by First into g
select g;
Automatically converts return value to first element in sequence. Returns default value if sequence is empty.
var first = from i in items
group i by FirstOrDefault into g
select g;
Automatically converts return value to last element in sequence. Throws exception if sequence is empty.
var last = from i in items
group i by Last into g
select g;
Automatically converts return value to last element in sequence. Returns default value if sequence is empty.
var last = from i in items
group i by LastOrDefault into g
select g;
Automatically converts return value to the only element in sequence. Throws exception if sequence doesn't contain exactly one element.
var value = from i in items
group i by Single into g
select g;
Automatically converts return value to the only element in sequence. Returns default value if sequence is empty, throws exception if sequence contains more than one element.
var value = from i in items
group i by SingleOrDefault into g
select g;
Converts sequence to true if sequence is not empty; to false otherwise.
var isNotEmpty = from i in items
group i by Any() into g
select g;
Converts sequence to true if any element satisfy the condition; to false otherwise.
var hasZero = from i in items
group i by Any(i == 0) into g
select g;
Converts sequence to true if all elements satisfy the condition; to false otherwise.
var hasNoZero = from i in items
group i by All(i != 0) into g
select g;
Converts sequence to number of elements.
var count = from i in items
group i by Count() into g
select g;
Converts sequence to a number that represents how many elements in the sequence satisfy the condition.
var countOfOddNumbers = from i in items
group i by Count(i % 2 == 1) into g
select g;
Converts sequence to maximum value in the sequence.
var max = from i in items
group i by Max into g
select g;
Converts sequence to minimum value in the sequence.
var min = from i in items
group i by Min into g
select g;
Converts sequence to sum value of the sequence.
var sum = from i in items
group i by Sum into g
select g;
Converts sequence to average value of the sequence.
var avg = from i in items
group i by Average into g
select g;
Concatenates sequence using specified separator.
var str = from i in items
group i by StringJoin(", ") into g
select g;
Converts sequence to true if it contains specified value; to false otherwise.
var containsZero = from i in items
group i by Contains(0) into g
select g;
Converts sequence to true if it contains any of specified values; to false otherwise.
var containsTwoOrFive = from i in items
group i by ContainsAny(2, 5) into g
select g;
Converts sequence to true if it contains all of specified values; to false otherwise.
var containsTwoAndFive = from i in items
group i by ContainsAll(2, 5) into g
select g;
Converts sequence to true if current sequence equals second sequence; to false otherwise.
var equals = from i in items
group i by SequenceEqual(expectedItems) into g
select g;