你能告訴我Linq2SQL在哪裏對我的sql服務器執行查詢嗎?我希望在我映射到名爲Car
的域對象後發生這種情況。Linq-to-SQL何時運行查詢?
public class Car
{
public Guid CarId { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public int HorsePower { get; set; }
}
public class CarRepository
{
private readonly MyDataContext _dc;
private readonly Func<DbCar, Car> _mappedCar =
c => new Car
{
CarId = c.CarId,
HorsePower = c.HorsePower,
Make = c.Make,
Model = c.Model,
};
public CarRepository(MyDataContext dc)
{
_dc = dc;
}
public Car GetCar(Guid carId)
{
var car = _dc.GetTable<DbCar>()
.Select(_mappedCar)
.Single(c => c.CarId == carId);
return car;
}
}
這實際上工作嗎?如果是這樣,我印象深刻Linq-to-SQL可以解析你的''_mappedCar''函數並將該''c => c.CarId == carId''謂詞映射回db世界... –
是的,它作品。 –