Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解构LINQ中包含ValueTuple的容器_C#_Linq_Valuetuple - Fatal编程技术网

C# 解构LINQ中包含ValueTuple的容器

C# 解构LINQ中包含ValueTuple的容器,c#,linq,valuetuple,C#,Linq,Valuetuple,我能够通过扩展方法解构容器: 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 De

我能够通过扩展方法解构容器:

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();
我只是想知道如何(以及是否)在(任何)LINQ方法中解构ValueTuple。我想我必须为每个Linq方法(类似列表)+字典重载+ValueTuple中的每一个值添加一个扩展方法。
示例中Where()的情况如何?

因为您处理的是
字典
迭代的值是
KeyValuePair
s。您需要处理
KeyValuePair
部分,然后只使用值元组的命名属性

var failedLimits = limits.Where(kvp => kvp.Value.SupportedLimit < req);
var failedLimits=限制。其中(kvp=>kvp.Value.SupportedLimit
公共静态类LinqExtensions
{
公共静态IEnumerable在哪里(
这是一个数不清的来源,
Func谓词)
=>source.Where(谓词);
}
  • 列表类型的重载+ValueTuple参数的每一个数量

首先,您需要了解
字典是一个
IEnumerable
。以及
扩展方法在哪里工作。不,您不需要为每个Linq方法添加扩展方法。只需使用值元组的命名参数。这就是它们存在的全部原因。只有在将值元组分配给多个变量时(通常在方法返回时),才需要解构。在lambda表达式中不需要它,至少我看不到。还要确保在正确的位置使用值元组。其他选项包括引用元组、匿名类或只是一个普通的旧自定义类。每一个都有一个首选用例。具体来说,值元组对于清除代码中的参数非常有用,因此您可能应该在扩展方法中使用它们作为返回类型。@juharr我发现
limits.Where(((uu,sup,req)=>sup
限制更可读/简洁。其中(kvp=>kvp.Value.RequiredLimit
var failedLimits = limits.Where(kvp => kvp.Value.SupportedLimit < req);
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 );
}