Giter VIP home page Giter VIP logo

linq.extras's Introduction

Linq.Extras

Full API documentation is available:

Install from NuGet:

PM> Install-Package Linq.Extras 

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. Append, 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

thomaslevesque avatar lbragaglia avatar

Watchers

James Cloos avatar Tushar Koshti avatar

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.