I'm trying to use a lambda expression to compare each element in a hashset to all of the others.
Here is an example of what I am trying to do. I have a class of type Implication. Implication has two properties - antecedent and consequent. If one implication says that A implies B and another says that B implies C, then there is a transitive relationship. In other words A implies C. Here is a simplified version of my code.
I'm trying to use a lambda expression to find all of the Implication objects within a hashset that have transitive relationships. In the last line of code in this class, I am using a Where clause to do the query. But, I'm getting error message. For some reason, it expects my second parameter (otherImplication) to be of type int instead of Implication. But, the first parameter is interpreted correctly. How do I tell it what type the second parameter is?
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 ?)
}
}
Thanks for your help.