LINQ to Entities类似的代码

时间:2017-06-10 19:48:14

标签: c# entity-framework linq linq-to-entities

我有类似的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
{

}

任何想要编写方法的人都会像这样返回Expression:

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对象......

2 个答案:

答案 0 :(得分:2)

如果ComdataFuelDetailVM和ComdataFuelDeleteVM具有相同的属性,那么只需为包含所有这些属性的类编写公共接口或基类,并在您的方法中使用此构造:

private Expression<Func<ComdataFuel, T>> GetPictureExpression<T>() where T : ICommonInterface

答案 1 :(得分:2)

private Expression<Func<ComdataFuel, T>> GetPictureExpression<T>() where T: ComdataFuelAbstractVM, new()

创建T你必须添加new()约束