У меня есть 2 модели,
public class Bin : INotifyPropertyChanged
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public IList<Produit> Produits
{
get;
set;
}
}
И
public class Produit : INotifyPropertyChanged
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public bool Actif
{
get
{
return _Actif;
}
set
{
if (_Actif != value)
{
_Actif = value;
RaisePropertyChanged("Actif");
}
}
}
}
В моей ViewModel я пытаюсь включить Produit, только если свойство "Actif" имеет значение true:
Bin bins = new ObservableCollection<Bin>(await db.Bins.Include(b=>b.Produits).Where(b => b.Produits.Count() > 0).ToListAsync());
Если я использую:
Bin bins = new ObservableCollection<Bin>(await db.Bins.Include(b => b.Produits.Where(p => p.Actif)).Where(b => b.Produits.Count() > 0).ToListAsync());
Я получил ошибку вроде:
System.ArgumentException: 'The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
Как я могу загружать только продукты "Actif", когда я включаю "Produit" в свой запрос.
Это прекрасно работает:
bins = new ObservableCollection<Bin>(db.Bins.Where(b => b.Produits.Count() > 0).Select(p => new
{
p,
Produit = p.Produits.Where(pp => pp.Actif)
})
.AsEnumerable()
.Select(x => x.p)
.ToList()
);
Единственная проблема заключается в том, что он загружает сущность в любом случае, но с нулевым атрибутом. Но если IF исправит проблему.
Спасибо за вашу помощь!