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从iGroup中选择具有不同变量值的Many_C#_Linq - Fatal编程技术网

C# Linq从iGroup中选择具有不同变量值的Many

C# Linq从iGroup中选择具有不同变量值的Many,c#,linq,C#,Linq,我正在尝试使用LINQ,我需要一些关于lambda表达式的帮助 我有一个I分组。选项类有多个变量,如价格,根等 我试图仅选择foreach中具有不同根值的选项对象,我不确定如何获取它。我试着这样做: IGrouping<DateTime, Option> optvalues; foreach (var symbol in optvalues.SelectMany(t => t.Root.Distinct())) { //This is returning some r

我正在尝试使用LINQ,我需要一些关于lambda表达式的帮助

我有一个
I分组
选项
类有多个变量,如
价格

我试图仅选择
foreach
中具有不同
值的
选项
对象,我不确定如何获取它。我试着这样做:

IGrouping<DateTime, Option> optvalues;
foreach (var symbol in optvalues.SelectMany(t => t.Root.Distinct()))
{ 
    //This is returning some random value “85 S” in rootdiff.
}
i分组选项值;
foreach(optvalues.SelectMany中的var符号(t=>t.Root.Distinct())
{ 
//这是在rootdiff中返回一些随机值“85 S”。
}

听起来你需要
区别对待

foreach (var symbol in optvalues.DistinctBy(opt => opt.Root)) {
}
这是一种扩展方法:

public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> src, Func<T, TKey> keyFun) {
    //      return src.GroupBy(keyFun).Select(g => g.FirstOrDefault());  // to defer execution
    var seenKeys = new HashSet<TKey>();
    foreach (T e in src)
        if (seenKeys.Add(keyFun(e)))
            yield return e;
}
公共静态IEnumerable DistinctBy(此IEnumerable src,Func keyFun){
//返回src.GroupBy(keyFun)。选择(g=>g.FirstOrDefault());//以推迟执行
var seenKeys=newhashset();
foreach(在src中)
如果(seenKeys.Add(keyFun(e)))
收益率e;
}

所以您只是将
Distinct
应用于错误的值

IGrouping<DateTime, Option> optvalues;
foreach (var symbol in optvalues.SelectMany(t => t).Select(t => t.Root).Distinct())
{ 
    //
}
i分组选项值;
foreach(optvalues.SelectMany(t=>t.Select)(t=>t.Root.Distinct()中的变量符号)
{ 
//
}

Root的类型是什么?它是一个字符串@khoroshevjSo
Root.Distinct()
将字符串视为
IEnumerable
,并返回
Root
:)中的不同字符(类型在C中非常重要)。如果您可以提供一个,即一个控制台应用程序,其中包含其他人可以运行的代码,那就太好了。您缺少用于说明这如何没有产生您期望的输出的输入,以及对该期望的充分描述。此结果与我得到的结果相同。85'S'因为
t.Root
是类型
string
SelectMany
没有意义-结果是
IEnumerable
。更好,但他想选择
选项
对象,而不是
Root
值。
    foreach (var symbol in optvalues.Select(t => r.Root).Distinct())
    { 
       //...
    }