表1 CAR(CARID,CarModelID,CARuniqueinfo1,CARUniqueinfo2):已将所有车辆输入仓库。
表2 CarModel(CarModelID,Modelinfo1,Modelinfo2)
表3销售(SaleID,CarID,clientID,EmpID)
返回的Linq查询是什么:
仅适用于ModelID = 101
的售出车辆数量DataClassesDataContext db = new DataClassesDataContext();
答案 0 :(得分:1)
简单方法:
var result =
db.Sale
.GroupBy(item => item.CarId)
.Select(item => new { item.Key, item.Count() });
很好的方式:
var result =
from sale in db.Sale
join car in db.CAR
on car.CARID equals sale.CarID
join carModel in db.CarModel
on carModel.CarModelID equals car.CarModelID
select new
{
Model = carModel,
SoldCars = db.Sale.Count(item => item.CarID == car.CARID)
};
答案 1 :(得分:0)
假设您有Sale(CarID)
的导航属性:
int all = db.Models
.Where(m => m.Cars.SelectMany(c => c.Sales).Any())
.Select(m => new {m.ModelID, m.Cars.Count}).ToArray();
答案 2 :(得分:0)
var counts = Model.GroupBy(m => m.CarModelID)
.Select(grp => ModelId = grp.Key, Count = grp.Count())
答案 3 :(得分:0)
此:
Dictionary<int, int> carsSold = new Dictionary<int, int>();
foreach (Car in Cars) {
carrsold.Add(x => x.Id, Sales.Where(y => y.CarId == x.Id).Count());
}
会给你留下一张字典,用于将汽车ID映射到售出金额。
carsSold[1]
将返回ID为1的汽车销售的金额。
答案 4 :(得分:0)
private int GetCarsSoldCount(int modelId)
{
return db.Sale.Count(x => x.CarID == modelId);
}
答案 5 :(得分:0)
var r = from car in CarTable
from sale in SalesTable
where sale.CarID == car.CarID
group sale by car.CarModelID into g
select new { Model = g.Key, Count = g.Count() };