The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

LINQ::DSL - alternative syntax for LINQ

ABSTRACT

    use LINQ::DSL ':default_safe';
    
    my @people = (
        { name => "Alice", dept => 8 },
        { name => "Bob",   dept => 7, this_will => 'be ignored' },
        { name => "Carol", dept => 7 },
        { name => "Dave",  dept => 8 },
        { name => "Eve",   dept => 1 },
    );
    
    my @depts = (
        { dept_name => 'Accounts',  id => 1 },
        { dept_name => 'IT',        id => 7 },
        { dept_name => 'Marketing', id => 8 },
    );
    
    my $collection = Linq {
        From \@people;
        SelectX 'name', 'dept';
        LeftJoin \@depts, field('dept'), field('id'), HashSmush;
        OrderBy -string, field('name');
        Cast AutoObject;
    };
    
    $collection->foreach( sub {
        printf "%s from %s\n", $_->name, $_->dept_name;
    } );

DESCRIPTION

This module allows you to create and manipulate LINQ::Collection objects using functions instead of chained method calls. The result is a fairly SQL-like syntax.

Linq {...} returns a LINQ::Collection unless the block includes an aggregating keyword (which must be the final statement in the block). An aggregating keyword will cause Linq to return the result of that keyword, which is usually a scalar, except in the case of the keyword ToList.

:essential

These can be imported using use LINQ::DSL ':essential'.

Linq { BLOCK }
From \@array
From sub { ITERATOR }
From range => [ $start, $end ]
From repeat => [ $value, $count ]
Where { CONDITION }
Select { EXPRESSION }
Join $collection, $hints, $leftexpr, $rightexpr, $joinexpr
GroupBy { EXPRESSION }
ForEach { BLOCK }
DefaultIfEmpty $value

Additionally, :essential includes HashSmush( $href1, $href2 ). This combines multiple hashrefs into a single hashref and then converts that to a blessed object using Object::Adhoc. If the hashrefs contain overlapping keys, the first one "wins".

:join

These can be imported using use LINQ::DSL ':join'.

LeftJoin $collection, $leftexpr, $rightexpr, $joinexpr
RightJoin $collection, $leftexpr, $rightexpr, $joinexpr
InnerJoin $collection, $leftexpr, $rightexpr, $joinexpr
OuterJoin $collection, $leftexpr, $rightexpr, $joinexpr
GroupJoin $collection, $hints, $leftexpr, $rightexpr, $joinexpr

:sort

These can be imported using use LINQ::DSL ':sort'.

OrderBy $hints, sub { EXPRESSION }
OrderByDescending $hints, sub { EXPRESSION }
Reverse

:filter

These can be imported using use LINQ::DSL ':filter'.

Distinct { EXPRESSION }
Take $count
TakeWhile { EXPRESSION }
Skip $count
SkipWhile { EXPRESSION }

:native

These can be imported using use LINQ::DSL ':native'. These keywords are aggregating keywords!

ToList
ToArray
ToDictionary { KEY EXPRESSION }
ToIterator

:aggregate

These can be imported using use LINQ::DSL ':aggregate'. These keywords are aggregating keywords!

Min { EXPRESSION }
Max { EXPRESSION }
Sum { EXPRESSION }
Average { EXPRESSION }
Aggregate { EXPRESSION }
Any { TRUTH EXPRESSION }
All { TRUTH EXPRESSION }
Contains $item, sub { COMPARATOR }
Count
SequenceEqual $other_collection

Any and All are very generic-sounding keywords, and Any conflicts with the function of the same name from Types::Standard, so use LINQ::DSL ':aggregate_safe' can be used to avoid importing those functions.

:field

These can be imported using use LINQ::DSL ':field'.

SelectX @fields
WhereX @checks
field $name
fields @fields
check_fields @checks

See LINQ::Util. SelectX combines Select and fields. WhereX combines Where and check_fields.

:type

These can be imported using use LINQ::DSL ':type'.

Cast $type
Cast AutoObject
OfType $type

:combine

These can be imported using use LINQ::DSL ':combine'.

Concat $collection
Union $collection, sub { COMPARATOR }
Intersect $collection, sub { COMPARATOR }
Except $collection, sub { COMPARATOR }
Zip $collection, sub { EXPRESSION }

:get

These can be imported using use LINQ::DSL ':get'. These keywords are aggregating keywords!

First { TRUTH EXPRESSION }
FirstOrDefault { TRUTH EXPRESSION } $default
Last { TRUTH EXPRESSION }
LastOrDefault { TRUTH EXPRESSION } $default
Single { TRUTH EXPRESSION }
SingleOrDefault { TRUTH EXPRESSION } $default
ElementAt $index
ElementAtOrDefault $index, $default

BUGS

Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=LINQ.

SEE ALSO

LINQ::Collection, LINQ::Utils.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2022 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.