Home > database >  Linq include list of items
Linq include list of items

Time:10-19

I want to include a list of histories for the purchases that have a contract associated. The contract has a list of histories however I am only getting a single history how can I get all the histories for a contract

  var purchases = from purchase in _context.Purchases
                            join component in _context.Components on purchase.ComponentId equals component.ComponentId
                            join supplier in _context.Suppliers on component.SupplierId equals supplier.SupplierId
                            join contractDb in _context.Contracts on purchase.ContractId equals contractDb.ContractId into contract
                            from contractDb in contract.DefaultIfEmpty()
                            join histories in _context.Histories on purchase.ContractId equals histories.ContractId
                            where purchase.Step == 0
                            select new { component.ComponentId, purchase.SupplierId, supplier.Name, contractDb.ContractId, contractDb.ContractNumber, histories };
            

here is my model for Contract

public class Contract
{
    [Key]
    public UInt64 ContractId { get; set; }
    public string ContractNumber { get; set; }
    public string InitialContractNumber { get; set; }
    [ForeignKey("ClienteId")]
    public UInt64 ClienteId { get; set; }
    [ForeignKey("ContactId")]
    public UInt64? ContactId { get; set; }
    [ForeignKey("ComercialEmployeeId")]
    public UInt64 ComercialEmployeeId { get; set; }
    [ForeignKey("TechnicalEmployeeId")]
    public UInt64? TechnicalEmployeeId { get; set; }
    public ulong OfferId { get; set; }
    public string Brand { get; set; }
    public string Type { get; set; }
    public string SerialNumber { get; set; }
}

here is my model for histories

public class History
{
    public UInt64 ContractId { get; set; }
    public UInt64 StepId { get; set; }
    [DataType(DataType.DateTime)]
    public DateTime? InitialDate { get; set; }
    [DataType(DataType.DateTime)]
    public DateTime? FinalDate { get; set; }
    [DataType(DataType.DateTime)]
    public DateTime? Deadline { get; set; }
    public string EmployeeUserName { get; set; }
    public UInt64 EmployeeId { get; set; }

    public virtual cfgEtapa Step { get; set; }
    public virtual Contract Contract { get; set; }
}

CodePudding user response:

Try the following query:

 var purchases = 
    from purchase in _context.Purchases
    join component in _context.Components on purchase.ComponentId equals component.ComponentId
    join supplier in _context.Suppliers on component.SupplierId equals supplier.SupplierId
    join contractDb in _context.Contracts on purchase.ContractId equals contractDb.ContractId into contract
    from contractDb in contract.DefaultIfEmpty()
    where purchase.Step == 0
    select new 
    { 
        component.ComponentId, 
        purchase.SupplierId, 
        supplier.Name, 
        contractDb.ContractId, 
        contractDb.ContractNumber, 
        histories = _context.Histories.Where(h => purchase.ContractId == h.ContractId).ToList()
    };

If you post all classes, probably we can remove joins.

  • Related