2010-01-20 2 views
2

Я вставляю свой код ниже;linq отдельные данные не дают конкретных результатов

Мой базовый класс переопределяет Equals и getHashcode, но запрос linq не возвращает отличные результаты. В результатах есть несколько городов с одинаковым идентификатором.

public class Product : EntityBase 
    { 
     public virtual string Name { get; set; } 

     public virtual IList<ProductDayDefinition> Days { get; set; } 
    } 

    public class ProductDayDefinition : EntityBase 
    { 
     public virtual Product Product { get; set; } 

     public virtual City City { get; set; } 

    } 


public abstract class EntityBase 
    { 
     public virtual int ID { get; protected internal set; } 

     protected EntityBase() : this(0) 
     { 

     } 

     protected EntityBase(int ID) 
     { 
      this.ID = ID; 

      if (this.ID == null) 
       this.ID = 0; 
     }   

     #region Equals definitions 
     public override bool Equals(object entity) 
     { 
      return entity != null 
       && entity is EntityBase 
       && this == (EntityBase)entity; 
     } 

     public static bool operator ==(EntityBase base1, EntityBase base2) 
     { 
      if ((object)base1 == null && (object)base2 == null) 
       return true; 

      if ((object)base1 == null || (object)base2 == null) 
       return false; 

      if (base1.ID != base2.ID) 
       return false; 

      return true; 
     } 

     public static bool operator !=(EntityBase base1, EntityBase base2) 
     { 
      return (!(base1 == base2)); 
     } 

     public override int GetHashCode() 
     { 
      return this.ID.GetHashCode(); 
     } 
     #endregion 
    } 

var cities = (from product in NHibernateSession.Linq<Product>() 
       from day in product.Days 
       where day.City != null 
       select day).Distinct(); 
+0

Вы 'выберите day' но вар называется' cities'. Каков ваш ожидаемый результат? –

ответ

2

запрос выполняется на стороне сервера (в базе данных). переопределить равные или gethashcode не имеет никакого значения.

Просто выберите атрибут id, а затем вызовите Distinct в конце. Вам придется перебирать результат, чтобы получить дополнительную информацию.

ИЛИ

Вы можете использовать объединение, чтобы получить детали идентификаторов вернулись из подзапроса.

http://msdn.microsoft.com/en-us/library/cc716801.aspx

+0

будет ли решение моей проблемы? – GuestMVCAsync

Смежные вопросы