C# 这就是你的问题。选项1或3可能是您的最佳选择。ORM对象很可能是可变的,而且通常不希望覆盖Equals或GetHashCode方法。啊,好的,这更有意义。我将尝试选项1,看看它是否有效,谢谢。@user1911333您对AssociatedAttribut
C# 这就是你的问题。选项1或3可能是您的最佳选择。ORM对象很可能是可变的,而且通常不希望覆盖Equals或GetHashCode方法。啊,好的,这更有意义。我将尝试选项1,看看它是否有效,谢谢。@user1911333您对AssociatedAttribut,c#,linq,telerik-open-access,C#,Linq,Telerik Open Access,这就是你的问题。选项1或3可能是您的最佳选择。ORM对象很可能是可变的,而且通常不希望覆盖Equals或GetHashCode方法。啊,好的,这更有意义。我将尝试选项1,看看它是否有效,谢谢。@user1911333您对AssociatedAttributes的实现正是servy在其答案中发布的内容。如果仍要保留该实现,则可能必须实现IEqualityComparer而不是IComparable。 int numMatchingAttributes = p.AssociatedAttribute
这就是你的问题。选项1或3可能是您的最佳选择。ORM对象很可能是可变的,而且通常不希望覆盖
Equals
或GetHashCode
方法。啊,好的,这更有意义。我将尝试选项1,看看它是否有效,谢谢。@user1911333您对AssociatedAttributes
的实现正是servy在其答案中发布的内容。如果仍要保留该实现,则可能必须实现IEqualityComparer
而不是IComparable
。
int numMatchingAttributes = p.AssociatedAttributes.Intersect( p.AssociatedAttributes ).Distinct( ).Count( );
public int CompareTo(Attribute other)
{
return Id.CompareTo(other.Id);
}
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;
}
}
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;
}
}
public class Page
{
public List<Attribute> AssociatedAttributes
{
get
{
return new List<Attribute>() {
new Attribute { Value = "a" },
new Attribute { Value = "b" },
new Attribute { Value = "c" },
};
}
}
}