There are two data models:

public class EquipmentProduktModel { public int Id { get; set; } public int EquipmentModelId { get; set; } public virtual EquipmentModel EquipmentModels { get; set; } public int ProduktId { get; set; } public virtual Produkt Produkt { get; set; } public decimal Quantity { get; set; } } public class Produkt { public int Id { get; set; } [Required(ErrorMessage = "Введите названия продукта")] [StringLength(450, MinimumLength = 3, ErrorMessage = "Название должно быть от 1 до 450 символов")] [Display(Name = "Название")] public string Name { get; set; } [Display(Name = "Примечание")] public string Note { get; set; } [Display(Name = "Артикул")] public string Articul { get; set; } [Display(Name = "Штрих код")] public string Barcode { get; set; } [Display(Name = "Единицы измерения")] public virtual Unit Units { get; set; } [Display(Name = "Минимальное количество")] public decimal MinQuantity { get; set; } [Display(Name = "Минимальный заказ")] public decimal MinZakaz { get; set; } public ICollection<EquipmentProduktModel> EquipmentProduktModels { get; set; } [Display(Name = "Учитовать в браке")] public bool BrakOn { get; set; } } 

How to create a collection of Produkts in which the records already added by EquipmentProduktModel are excluded.

When we form a request:

 var List = from p in _context.Produkt from s in _context.EquipmentProduktModel.Where(c=>c.EquipmentModelId == 1012) where p.Id == s.ProduktId select p; 

That request will create a collection of those records that already exist in EquipmentProduktModel.

But if we change

  where p.Id != s.ProduktId 

That only the first position from the EquipmentProduktModel collection is excluded from the list.

How to create a collection of Produkts correctly excluding those items that have already been made to EquipmentProduktModel.

  • Use .Distinct() . - user218976
  • Can I have a little more detail? - blakcat

1 answer 1

I have nowhere to check, but maybe so

 var List = from p in _context.Produkt where !_context.EquipmentProduktModel .Where(c=>c.EquipmentModelId == 1012) .Any(m=>m.ProduktId == p.Id) select p; 

or so

 var ids = _context.EquipmentProduktModel .Where(c => c.EquipmentModelId == 1012) .Select(c=>c.Id); var List = from p in _context.Produkt where !ids.Contains(p.Id) select p; 

or so

 var List = from p in _context.Produkt where !_context.EquipmentProduktModel .Where(c => c.EquipmentModelId == 1012) .Select(c => c.Id).Contains(p.Id) select p; 

not sure about the effectiveness of requests, but it seems to be correct. If there are errors, sorry, did not work with the database for a long time.