Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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# 比较同一集合中两个元素的lambda表达式的语法是什么?_C#_Lambda_Hashset - Fatal编程技术网

C# 比较同一集合中两个元素的lambda表达式的语法是什么?

C# 比较同一集合中两个元素的lambda表达式的语法是什么?,c#,lambda,hashset,C#,Lambda,Hashset,我尝试使用lambda表达式将哈希集中的每个元素与所有其他元素进行比较 下面是我正在尝试做的一个例子。我有一类类型蕴涵。蕴涵有两个性质——先行和结果。如果一个暗示说A暗示B,而另一个暗示说B暗示C,那么就存在传递关系。换句话说,A意味着C。这里是我的代码的简化版本 我试图使用lambda表达式来查找hashset中具有传递关系的所有隐含对象。在这个类的最后一行代码中,我使用Where子句进行查询。但是,我收到了错误消息。出于某种原因,它希望我的第二个参数(OtherImpression)的类型为

我尝试使用lambda表达式将哈希集中的每个元素与所有其他元素进行比较

下面是我正在尝试做的一个例子。我有一类类型蕴涵。蕴涵有两个性质——先行和结果。如果一个暗示说A暗示B,而另一个暗示说B暗示C,那么就存在传递关系。换句话说,A意味着C。这里是我的代码的简化版本

我试图使用lambda表达式来查找hashset中具有传递关系的所有隐含对象。在这个类的最后一行代码中,我使用Where子句进行查询。但是,我收到了错误消息。出于某种原因,它希望我的第二个参数(OtherImpression)的类型为int而不是impression。但是,第一个参数的解释是正确的。我如何告诉它第二个参数是什么类型

public class Implication
{
    public int antecedent  { get; set; }
    public int consequent  { get; set; }

    public Implication(int antecedent, int consequent)
    {
        this.antecedent  = antecedent;
        this.consequent  = consequent;
    }

    public static void Test()
    {
        HashSet<Implication> hashset = new HashSet<Implication>();
        hashset.Add(new Implication(1, 2));  //transitive
        hashset.Add(new Implication(2, 3));  //transitive
        hashset.Add(new Implication(3, 4));  //transitive
        hashset.Add(new Implication(5, 6));  //NOT transitive
        hashset.Add(new Implication(7, 8));  //NOT transitive

        var transitives = hashset.Where((implication, otherimplication) => implication.antecedent  == otherimplication.consequent);

        // I'm getting the following error message for 
        // 'otherimplication.consequent' at the end of the previous line.

        // Error CS1061  'int' does not contain a definition for 
        // 'consequent' and no extension method 'consequent' 
        // accepting a first argument of type 'int' could be 
        // found(are you missing a using directive or an 
        // assembly reference ?)    

    }
}
公共类蕴涵
{
公共int先行项{get;set;}
公共int结果{get;set;}
公共含义(int先行项,int后继项)
{
this.antecedent=先行项;
this.continued=continued;
}
公共静态无效测试()
{
HashSet HashSet=新的HashSet();
Add(新含义(1,2));//可传递
Add(新含义(2,3));//可传递
Add(新含义(3,4));//可传递
Add(新含义(5,6));//不可传递
Add(新含义(7,8));//不可传递
var-transitives=hashset.Where((暗示,其他暗示)=>implication.antecedent==otherimplication.continuent);
//我收到以下的错误消息
//前一行末尾的“OtherImpression.Consultant”。
//错误CS1061“int”不包含的定义
//“结果”和无扩展方法“结果”
//接受类型为“int”的第一个参数可能是
//找到(您是否缺少using指令或
//装配参考?)
}
}
谢谢你的帮助。

试试这个:

    var antecedents = hashset.ToLookup(x => x.antecedent);
    var consequents = hashset.ToLookup(x => x.consequent);

    var transitives =
        hashset
            .Where(x =>
                antecedents[x.consequent]
                    .Concat(consequents[x.antecedent])
                    .Any());

这给了我
(1,2)、(2,3)、(3,4)

,其中
不用于比较元素,而是用于过滤序列。特别是这个版本的
,其中
从序列及其索引中获取给定的元素,因此第二个参数是
int
“我如何告诉它第二个参数是什么类型?”这不是.NET中编程的工作方式。该方法定义了它想要/需要的参数(作为其方法声明的一部分)。每个参数的用途在相应方法的文档中进行了解释(如果希望存在这样的文档)。您不会告诉一个方法您认为参数应该是什么。根据方法的声明和文档,该方法将期望(或可能是“需求”)提供有意义的和有效的参数参数。因为@ elgZo指出的是扩展方法“可枚举”的文档。因此,(你)认为以下顺序可传递性,(1,4),(2,3),(4,5),(5,2),(3,6)。?不清楚预期输出应该是什么。您想要所有可能的传递组吗?那就
(1,2)、(2,3)
?这被认为是可传递的(即使
(1,3)
没有明确列在列表中?那么上面@TheGeneral的评论呢?这是一组可传递的项目,还是您希望所有不同的隐式可传递项目也显示出来(即
(1,5),(1,2),(1,3),(1,6),(4,2),等等。