1
  • Table1 CAR (CARID, CarModelID ,CARuniqueinfo1, CARUniqueinfo2):contians 所有的汽车都已入库。

  • 表2 CarModel (CarModelID ,Modelinfo1 ,Modelinfo2 )

  • 表 3 销售(SaleID、CarID、clientID、EmpID)

返回的 Linq 查询是什么:

仅针对 ModelID=101 的已售汽车数量

DataClassesDataContext db = new DataClassesDataContext(); 
4

6 回答 6

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)
    };
于 2012-11-07T13:49:03.267 回答
0
var counts = Model.GroupBy(m => m.CarModelID)
                  .Select(grp => ModelId = grp.Key, Count = grp.Count())
于 2012-11-07T13:50:20.710 回答
0
private int GetCarsSoldCount(int modelId)
{
  return db.Sale.Count(x => x.CarID == modelId);
}
于 2012-11-07T13:58:55.170 回答
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() };
于 2012-11-07T13:58:59.327 回答
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();
于 2012-11-07T13:44:13.523 回答
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 的汽车售出的金额。

于 2012-11-07T13:52:45.380 回答