1

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

2 Answers 2

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

to create T you have to add new() constraint

Sign up to request clarification or add additional context in comments.

Comments

2

If ComdataFuelDetailVM and ComdataFuelDeleteVM have the same properties then just write common interface or base class for these classes containing all these properties and in your method use this construction:

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

4 Comments

It's not a rule. These classes can have different superclasses, but have the same properties... Also, I want to have a flexible solution if classes have a little difference (i.e. one property) etc
So just use common interface for them, not common superclass. Common interface will contain all properties which are shared by these classes and it will allow to write generic method in way that i have provided
Interfaces will increase subclasses and add difficulties to add/remove new properties..
I don't think that there is another way to do it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.