LINQ to Entities 相似代码
LINQ to Entities similar code
我有类似的 LINQ to Entities 请求:
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();
等等
ComdataFuelDetailVM 和 ComdataFuelDeleteVM 具有相同的属性(但用作不同视图的视图模型):
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
{
}
任何想写一个方法,其中returns表达式是这样的:
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;
}
但我无法创建 T 对象...
如果 ComdataFuelDetailVM 和 ComdataFuelDeleteVM 具有相同的属性,那么只需为这些包含所有这些属性的 class 编写通用接口或基础 class,并在您的方法中使用此结构:
private Expression<Func<ComdataFuel, T>> GetPictureExpression<T>() where T : ICommonInterface
private Expression<Func<ComdataFuel, T>> GetPictureExpression<T>() where T: ComdataFuelAbstractVM, new()
要创建 T,您必须添加 new() 约束
我有类似的 LINQ to Entities 请求:
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();
等等
ComdataFuelDetailVM 和 ComdataFuelDeleteVM 具有相同的属性(但用作不同视图的视图模型):
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
{
}
任何想写一个方法,其中returns表达式是这样的:
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;
}
但我无法创建 T 对象...
如果 ComdataFuelDetailVM 和 ComdataFuelDeleteVM 具有相同的属性,那么只需为这些包含所有这些属性的 class 编写通用接口或基础 class,并在您的方法中使用此结构:
private Expression<Func<ComdataFuel, T>> GetPictureExpression<T>() where T : ICommonInterface
private Expression<Func<ComdataFuel, T>> GetPictureExpression<T>() where T: ComdataFuelAbstractVM, new()
要创建 T,您必须添加 new() 约束