I have an object called Page, with an instance called p that has a custom property called AssociatedAttributes. If I do the following:
int numMatchingAttributes = p.AssociatedAttributes.Intersect( p.AssociatedAttributes ).Distinct( ).Count( );
numMatchingAttributes ends up equaling 0 even though p has 6 AssociatedAttributes. Why does it not equal 6?
AssociatedAttributes
is of Type List<Attribute>
(Attribute
is my own class, not System.Attribute
) and Attribute
implements IComparable<Attribute>
, I did not have it implement IEquatable
, should I?
This is the implementation of CompareTo
in Attribute:
public int CompareTo(Attribute other)
{
return Id.CompareTo(other.Id);
}
Id
is of type Guid
.
this is the AssociatedAttributes property on Page:
public List<Attribute> AssociatedAttributes
{
get
{
List<Attribute> list = new List<Attribute>( );
using (
PredictiveRecommendor dbContext =
new PredictiveRecommendor()){
if ( dbContext != null )
{
IQueryable<Attribute> query = from a in dbContext.PageAttributes
where a.Page.Id.Equals(this.Id)
select a.Attribute;
list = query.ToList();
}
}
return list;
}
}
(dbContext is a Telerik OpenAccess context)
Update: Here is what ended up working: In Page is the following method:
public int numberOfIntersectedAssociatedAttributes ( Page other )
{
using ( PredictiveRecommendor dbContext = new PredictiveRecommendor( ) )
{
IQueryable<Attribute> thisAssocAttributes = from a in dbContext.PageAttributes
where a.Page.Id.Equals( this.Id )
select a.Attribute;
IQueryable<Attribute> otherAssocAttributes = from a in dbContext.PageAttributes
where a.Page.Id.Equals( other.Id )
select a.Attribute;
IQueryable<Attribute> interSection = thisAssocAttributes.Intersect( otherAssocAttributes );
return interSection.ToList( ).Count;
}
}
It's ugly, but it works.