I have similar LINQ to Entities requests:
ComdataFuelDetailVM model = (from i in db.ComdataFuels
where i.ID == id.Value
select new ComdataFuelDetailVM()
{
ID = i.ID,
CardNo = i.CardNo,
City = i.City,
CompanyId = i.CompanyId,
DriverId = i.DriverId,
DriverName = i.DriverName,
EmployeeNo = i.EmployeeNo,
Fees = i.Fees,
GalPrice = i.GalPrice,
NoOfGal = i.NoOfGal,
OwnerOp = i.OwnerOp,
PayoutRebate = i.PayoutRebate,
Rebate = i.Rebate,
State = i.State,
TotalCost = i.TotalCost,
TransDate = i.TransDate,
TransTime = i.TransTime,
TruckNo = i.TruckNo,
TruckStopCode = i.TruckStopCode,
TruckStopInvoiceNo = i.TruckStopInvoiceNo,
TruckStopName = i.TruckStopName,
UnitNo = i.UnitNo
}).FirstOrDefault();
ComdataFuelDeleteVMmodel = (from i in db.ComdataFuels
where i.ID == id.Value
select new ComdataFuelDeleteVM()
{
ID = i.ID,
CardNo = i.CardNo,
City = i.City,
CompanyId = i.CompanyId,
DriverId = i.DriverId,
DriverName = i.DriverName,
EmployeeNo = i.EmployeeNo,
Fees = i.Fees,
GalPrice = i.GalPrice,
NoOfGal = i.NoOfGal,
OwnerOp = i.OwnerOp,
PayoutRebate = i.PayoutRebate,
Rebate = i.Rebate,
State = i.State,
TotalCost = i.TotalCost,
TransDate = i.TransDate,
TransTime = i.TransTime,
TruckNo = i.TruckNo,
TruckStopCode = i.TruckStopCode,
TruckStopInvoiceNo = i.TruckStopInvoiceNo,
TruckStopName = i.TruckStopName,
UnitNo = i.UnitNo
}).FirstOrDefault();
etc
ComdataFuelDetailVM and ComdataFuelDeleteVM have the same properties (but used as view models for different views):
public abstract class ComdataFuelAbstractVM
{
public int ID { get; set; }
public int? CompanyId { get; set; }
public string TruckNo { get; set; }
public int? DriverId { get; set; }
[Display(Name = "Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime TransDate { get; set; }
public string TransTime { get; set; }
public string DriverName { get; set; }
public string UnitNo { get; set; }
public string City { get; set; }
public string State { get; set; }
public string TruckStopCode { get; set; }
public string TruckStopName { get; set; }
public string TruckStopInvoiceNo { get; set; }
public decimal? NoOfGal { get; set; }
[DisplayFormat(DataFormatString = "{0:C3}")]
public decimal? GalPrice { get; set; }
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal? TotalCost { get; set; }
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal? Fees { get; set; }
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal? Rebate { get; set; }
[Display(Name = "Rebate")]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal? PayoutRebate { get; set; }
public string CardNo { get; set; }
public string EmployeeNo { get; set; }
public bool? OwnerOp { get; set; }
}
public class ComdataFuelDetailVM : ComdataFuelAbstractVM
{
}
public class ComdataFuelDeleteVM : ComdataFuelAbstractVM
{
}
any want to write a method, which returns Expression like this:
private Expression<Func<ComdataFuel, T>> GetPictureExpression<T>()
{
Expression<Func<ComdataFuel, T>> projection = i =>
new T()
{
ID = i.ID,
CardNo = i.CardNo,
City = i.City,
CompanyId = i.CompanyId,
DriverId = i.DriverId,
DriverName = i.DriverName,
EmployeeNo = i.EmployeeNo,
Fees = i.Fees,
GalPrice = i.GalPrice,
NoOfGal = i.NoOfGal,
OwnerOp = i.OwnerOp,
PayoutRebate = i.PayoutRebate,
Rebate = i.Rebate,
State = i.State,
TotalCost = i.TotalCost,
TransDate = i.TransDate,
TransTime = i.TransTime,
TruckNo = i.TruckNo,
TruckStopCode = i.TruckStopCode,
TruckStopInvoiceNo = i.TruckStopInvoiceNo,
TruckStopName = i.TruckStopName,
UnitNo = i.UnitNo
};
return projection;
}
but I can't create T object...
private Expression<Func<ComdataFuel, T>> GetPictureExpression<T>() where T: ComdataFuelAbstractVM, new()
to create T you have to add new() constraint