Giter Club home page Giter Club logo

linq.extras's Introduction

Linq.Extras

NuGet version AppVeyor build AppVeyor tests Docs (online) Docs (offline)

A set of extension and helper methods to complement the ones from System.Linq.Enumerable.

Some of these methods are just shortcuts for common Linq operations (e.g. IsNullOrEmpty), or improvements to existing Linq methods (e.g. specify default value for FirstOrDefault, specify comparer for Max). Others do more complex things that have no equivalent in standard Linq (RankBy, DistinctUntilChanged).

Here are some methods of interest:

DistinctBy, IntersectBy, UnionBy, ExceptBy, SequenceEqualBy

Same as Distinct, Intersect, Union, Except, SequenceEqual, but allow you to specify a key for equality comparison.

var result = items.DistinctBy(i => i.Name);

DistinctUntilChanged

Returns a sequence with distinct contiguous items (i.e. removes contiguous duplicates).

var input = new[] { 1, 1, 1, 2, 3, 3, 1, 3, 2, 2, 1 };
var result = input.DistinctUntilChanged(); // 1, 2, 3, 1, 3, 2, 1

This is the enumerable equivalent of the Observable.DistinctUntilChanged method from Rx.

MinBy, MaxBy

Return the item of a sequence that has the min or max value for the specified key.

var winner = players.MaxBy(p => p.Score);
Console.WriteLine("The winner is {0} with {1} points!", winner.Name, winner.Score);

Unlike the well known approach of sorting the list and taking the first item, this method doesn't need sorting and operates in O(n).

RankBy, DenseRankBy

These methods associate a rank with each item of a collection, based on the specified key. The difference between the two is the same as between the RANK and DENSE_RANK functions in SQL: RankBy leaves "holes" in the ranks if some items are equal, while DenseRankBy does not.

This code:

var ranking = players.RankByDescending(player => player.Score, (player, rank) => string.Format("{0}. {1} ({2})", rank, player.Name, player.Score));

Produces the following results

1. Joe (42)
2. Liz (23)
2. Ben (23)
4. Ann (16)
5. Bob (15)

LeftOuterJoin, RightOuterJoin, FullOuterJoin

As the names imply.

The first two are for those who always forget how to do an outer join with Linq ;).

FullOuterJoin fills a gap, though, since there is no built-in way to do it with Linq.

var result = left.OuterJoin(right, x => x.Id, y => y.Id, (id, x, y) => new { x, y });

ToHierarchy

Transforms a flat sequence of items into a hierarchy. Each node is of type INode<T> and exposes its children and parent.

var roots = items.ToHierarchy(i => i.Id, i => i.ParentId);

Flatten

Transforms a hierarchy of objects to a flat sequence.

var flat = roots.Flatten(node => node.Children, TreeTraversalMode.DepthFirst);

linq.extras's People

Contributors

adamralph avatar lbragaglia avatar thomaslevesque avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

linq.extras's Issues

Investigate necessary changes for .NET 6.0

In .NET 6.0, Linq introduces a few new methods that could conflict with Linq.Extras methods or render them obsolete.

Hopefully avoid reproducing the issues that happened with ToHashSet (#19, #22)...

Nullable attributes for MinBy/MaxBy

It's a little unclear from the documentation what should be returned by MinBy and MaxBy when given an enumerable of null values. The regular LINQ Min and Max explicitly state that they return null in this case, which is very useful because we can use it with DefaultIfEmpty.

Should the documentation be more explicit? And if null is a valid return value that should probably be annotated with [return: MaybeNull].

[Runtime Error] MissingMethodException caused by using ToHashSet method

Today we crashed in too unpleasant bug in .NET Standard + Linq.Extras:

System.MissingMethodException Method not found: 'System.Collections.Generic.HashSet`1<!!0> Linq.Extras.XEnumerable.ToHashSet(System.Collections.Generic.IEnumerable`1<!!0>, System.Collections.Generic.IEqualityComparer`1<!!0>)'.

To reproduce it:

  1. Create a project NetStandartProject targeted to .netstandard2.0
  2. Add a method void SomeMethod(int[] a) => a.ToHashSet();
  3. Create a second project NetCoreProject targeted to .netcoreapp2.1 or higher
  4. Add NetStandartProject into NetCoreProject dependencies
  5. Call SomeMethod from NetCoreProject
  6. Get System.MissingMethodException

Here is a complete example solution: https://github.com/rodion-m/LinqExtrasRuntimeException. Just run NetCoreProject.

I suggest to delete all methods like ToHashSet from .netstandart target and let the developer write new HashSet(a) instead of a.ToHashSet() than crash in runtime with MissingMethodException. I think you just should make .netstandart version of Linq.Extras equal to .netcore version of Linq.Extras.

Ambiguous call for ToHashSet

Linq.Extras has a ToHashSet method, but an identical method was introduced in Linq in the following TFMs:

  • .NET Framework 4.7.2
  • .NET Core 2.0
  • .NET Standard 2.1

This causes an ambiguity when both the System.Linq and Linq.Extras namespaces are imported.

New targets should be added that exclude this method:

  netstandard1.0
  netstandard1.6
  netstandard2.0
+ netstandard2.1
  net45
  net471
+ net472
+ netcoreapp2.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.