C# Linq到实体和表达式
我有以下代码:C# Linq到实体和表达式,c#,entity-framework,linq,linq-to-entities,C#,Entity Framework,Linq,Linq To Entities,我有以下代码: 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")] [D
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
{
}
表达方式:
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;
}
private Expression getAdvancedDeclarationAbstractExpression(),其中T:AdvancedDeclarationAbstractVM,new()
{
表达式投影=i=>
新T()
{
ID=i.ID,
金额=i.金额,
DateTime=i.DateTime,
描述=i.描述,
DriverName=i.Driver.Name
};
回归投影;
}
并使用它:
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();
AdvancesDeclarationDetailVM模型=(从db.AdvancesDeclarations中的i开始)
其中i.ID==ID.Value
选择i)
.Select(GetAdvancesDeclarationAbstractExpression()).FirstOrDefault();
AdvancesDecretionDeleteVM模型=(来自db.AdvancesDecreations中的i
其中i.ID==ID.Value
选择i).选择(GetAdvancesDeclarationAbstractExpression()).FirstOrDefault();
它可以工作,但我想将其添加到Linq to实体中,而不是使用Lambda表达式。有什么方法可以使用它吗?您可能想看看 之后你就可以做了
[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
};
}
[lambda]
公共静态bool To(此AdvancedDeclarationAbstractVM值),其中T:AdvancedDeclarationAbstractVM,new()
{
抛出新的NotImplementedException();
}
公共静态表达式To(),其中T:AdvancedDeclarationAbstractVM,new()
{
返回i=>
新T()
{
ID=i.ID,
金额=i.金额,
DateTime=i.DateTime,
描述=i.描述,
DriverName=i.Driver.Name
};
}
像这样使用它
AdvancesDeductionDetailVM model = (from i in db.AdvancesDeductions.ToInjectable()
where i.ID == id.Value
select i.To<AdvancesDeductionDetailVM>()).FirstOrDefault();
AdvancesDeclarationDetailVM模型=(从db.AdvancesDeclarations.ToInjectable()中的i开始)
其中i.ID==ID.Value
选择i.To()).FirstOrDefault();
您可能想看看
之后你就可以做了
[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
};
}
[lambda]
公共静态bool To(此AdvancedDeclarationAbstractVM值),其中T:AdvancedDeclarationAbstractVM,new()
{
抛出新的NotImplementedException();
}
公共静态表达式To(),其中T:AdvancedDeclarationAbstractVM,new()
{
返回i=>
新T()
{
ID=i.ID,
金额=i.金额,
DateTime=i.DateTime,
描述=i.描述,
DriverName=i.Driver.Name
};
}
像这样使用它
AdvancesDeductionDetailVM model = (from i in db.AdvancesDeductions.ToInjectable()
where i.ID == id.Value
select i.To<AdvancesDeductionDetailVM>()).FirstOrDefault();
AdvancesDeclarationDetailVM模型=(从db.AdvancesDeclarations.ToInjectable()中的i开始)
其中i.ID==ID.Value
选择i.To()).FirstOrDefault();
您想将其添加到我想要的实体的linq中是什么意思(从db.advances中的i开始,其中i.ID==ID.Value选择MyFunction);不明白您的问题是什么?您想将它添加到linq中以获得我想要的实体是什么意思(从db.AdvancesDecreations中的i开始,其中i.ID==ID.Value选择MyFunction);不明白你的问题是什么?