I am trying to write the convert the SQL query to Linq but I am not sure if there is a way to write this query. Also, This query grab only matching value from CartonDetails table but i need all the value from Carton table.
select Carton.Id,
Carton.CartonNumber,
COUNT(CartonDetail.CartonId) as TotalCount
from Carton
Inner Join CartonDetail on CartonDetail.CartonId = Carton.Id
Group by Carton.Id, Carton.CartonNumber
That is what I have so far.I am new to the Linq. Thanks in advance
var apv = db.Cartons.Where(c => c.CartonDetails.Select(cd => cd.CartonId).Contains(c.Id)).GroupBy(c => c.Id, c => c.CartonNumber).Select(c => new CartonViewModel
{
Id = c.Key,
EquipmentCount = c.Count(),
// How can i select the Carton Number here.
});
return View(cartons);
}
CartonDetail.cs
[Table("CartonDetail")]
public partial class CartonDetail
{
public int Id { get; set; }
public int CartonId { get; set; }
public int EquipmentId { get; set; }
public Carton Carton { get; set; }
public Equipment Equipment { get; set; }
}