简体   繁体   中英

Deconstruct Container containing ValueTuple in LINQ

I am able to deconstruct a container via an extension method:

var limits = new[] { MyEnum.A , MyEnum.B , MyEnum.C }
        .ToDictionary( x => x ,
                       x => ( SupportedLimit: GetLimit( x ) , RequiredLimit: 0 ) );

public static class Extensions
{
    public static void Deconstruct<TKey, TVal>(
        this KeyValuePair<TKey , TVal> tuple ,
        out TKey key , out TVal value )
    {
        key = tuple.Key;
        value = tuple.Value;
    }

    public static void Deconstruct<TKey, TVal1, TVal2>(
        this KeyValuePair<TKey , (TVal1,TVal2)> tuple ,
        out TKey key , out TVal1 v1 , out TVal2 v2 )
    {
        key = tuple.Key;
        (v1 , v2) = tuple.Value;
    }
}

// works
foreach( var ( enumVal , supportedLimit , requiredLimit ) in limits )
    Debugger.Break();

How do I deconstruct a container/dictionary containing a System.ValueTuple in LINQ?

// won't work, .Where() expects Func<T,bool> not Func<T1,T2,T3,bool>

var failedLimits = limits.Where( ( _ , sup , req ) => sup < req );

I just wanted to know how (and if) it is possible to deconstruct the ValueTuple in (any) LINQ method. I guess I have to add an extension method for every Linq-method (List-like) + overloads for dictionaries + each amount of values in the ValueTuple. How would it look like for the Where() in the example?

public static class LinqExtensions
{
    public static IEnumerable<KeyValuePair<TKey,(T1,T2)>> Where<TKey,T1,T2>(
        this IEnumerable<KeyValuePair<TKey,(T1,T2)>> source ,
        Func<TKey,T1,T2, Boolean> predicate )
        => source.Where( predicate );
}
  • Overloads for List-like types + every amount of ValueTuple-parameter

Since you're dealing with a Dictionary the values you iterate over are KeyValuePair s. You need to deal with the Value part of the KeyValuePair and then just use the named property of your value tuple.

var failedLimits = limits.Where(kvp => kvp.Value.SupportedLimit < req);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM