Oleg Sh
Oleg Sh

Reputation: 9013

Linq to entities and Expression

I have the following code:

public abstract class AdvancedDeductionAbstractVM
{
    public int ID { get; set; }
    [Display(Name = "Driver Name")]
    public string DriverName { get; set; }
    public string Description { get; set; }
    [Display(Name = "Date")]
    [DisplayFormat(DataFormatString = "{0:d}")]
    public DateTime DateTime { get; set; }
    [Display(Name = "Amount")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal Amount { get; set; }
}

public class AdvancedDeductionElementVM : AdvancedDeductionAbstractVM
{
}

public class AdvancesDeductionDetailVM : AdvancedDeductionAbstractVM
{
}

public class AdvancesDeductionDeleteVM : AdvancedDeductionAbstractVM
{
}

expression:

    private Expression<Func<AdvancesDeduction, T>> GetAdvancesDeductionAbstractExpression<T>() where T : AdvancedDeductionAbstractVM, new()
    {
        Expression<Func<AdvancesDeduction, T>> projection = i =>
            new T()
            {
                ID = i.ID,
                Amount = i.Amount,
                DateTime = i.DateTime,
                Description = i.Description,
                DriverName = i.Driver.Name
            };
        return projection;
    }

and use it:

        AdvancesDeductionDetailVM model = (from i in db.AdvancesDeductions
                                           where i.ID == id.Value
                                           select i)
                                           .Select(GetAdvancesDeductionAbstractExpression<AdvancesDeductionDetailVM>()).FirstOrDefault();

        AdvancesDeductionDeleteVM model = (from i in db.AdvancesDeductions
                                           where i.ID == id.Value
                                           select i).Select(GetAdvancesDeductionAbstractExpression<AdvancesDeductionDeleteVM>()).FirstOrDefault();

It works, but I want to add it to Linq to entities, not using Lambda expression. Any way to use it?

Upvotes: 0

Views: 62

Answers (1)

Filip Cordas
Filip Cordas

Reputation: 2561

You might want to have a look at NeinLinq After that you can do

    [InjectLambda]
    public static bool To<T>(this AdvancedDeductionAbstractVM value) where T : AdvancedDeductionAbstractVM, new()
    {
        throw new NotImplementedException();
    }

    public static Expression<Func<AdvancedDeductionAbstractVM,T>> To<T>() where T : AdvancedDeductionAbstractVM, new()
    {
        return i =>
            new T()
            {
                ID = i.ID,
                Amount = i.Amount,
                DateTime = i.DateTime,
                Description = i.Description,
                DriverName = i.Driver.Name
            };
    }

And use it like

AdvancesDeductionDetailVM model = (from i in db.AdvancesDeductions.ToInjectable()

                                           where i.ID == id.Value
                                           select i.To<AdvancesDeductionDetailVM>()).FirstOrDefault();

Upvotes: 2

Related Questions