连接Linq表达式
我正在使用新的EF4 CTP4,尽管我不认为这与此有多大关系。我正在尝试建立一个系统,在这个系统中,我可以为数据库自动添加可审核字段。我要做的是把下面两个表达式组合起来连接Linq表达式,linq,entity-framework,Linq,Entity Framework,我正在使用新的EF4 CTP4,尽管我不认为这与此有多大关系。我正在尝试建立一个系统,在这个系统中,我可以为数据库自动添加可审核字段。我要做的是把下面两个表达式组合起来 a => new { a.CreatedBy, a.CreatedTime, a.UpdatedBy, a.UpdatedTime } 及 因此,结果相当于 a => new { a.Id, a.Name, a.CreatedBy, a.Created
a => new
{
a.CreatedBy,
a.CreatedTime,
a.UpdatedBy,
a.UpdatedTime
}
及
因此,结果相当于
a => new
{
a.Id,
a.Name,
a.CreatedBy,
a.CreatedTime,
a.UpdatedBy,
a.UpdatedTime
}
结果我需要一个表达式。我一直在摸索,尝试了一些关于表达式、调用和表达式以及(andalso)的方法,但没有发现任何适合我的方法
我不太确定这是否可行,但如果有任何帮助,我们将不胜感激。我认为您不能简单地“合并”两个表达式。但是您可以使用替代API创建与
EntityMap
的映射
public static class MapBuilder
{
public static Expression<Func<T, object>> GetMap<T>(Expression<Func<T, object>> func) where T: IAuditable
{
var body = func.Body as NewExpression;
var param = Expression.Parameter(typeof(T), "o");
var propertyAccessExprs = new List<Expression>();
foreach (MemberInfo member in body.Members)
{
propertyAccessExprs.Add(Expression.Property(param, member.Name));
}
var props = typeof(IAuditable).GetProperties();
foreach (PropertyInfo prop in props)
{
propertyAccessExprs.Add(Expression.Property(param, prop.Name));
}
var columnMappins = new List<Expression>();
foreach (var access in propertyAccessExprs)
{
columnMappins.Add(Expression.Call(typeof(EntityMap).GetMethod("Column", new Type[] {typeof(Object)}), Expression.Convert(access, typeof(Object))));
}
var RowExpr = Expression.Call(typeof(EntityMap).GetMethod("Row"), Expression.NewArrayInit(typeof(EntityMapColumn), columnMappins));
var result = Expression.Lambda<Func<T, object>>(RowExpr, param);
return result;
}
}
HTH.EF4已作为.NET 4.0的一部分正式发布。我指的是ADO.NET功能CTP4,它允许代码优先/仅代码ef开发。您想要一个具有以下签名的方法,对吗
Expression ProjectToAuditObject(),其中T:IAAudiTable
其中AuditObject
包含必需的属性(Id
,Name
等)。不完全是这样,我的T对象已经具有上面显示的所有属性,对象需要是匿名类型。我正在尝试为我的可审核字段轻松地将映射添加到数据库中。如果你已经播种了ef4 ctp。这是我试图调用MapSingleType(AuditablePropertyMap.Map(a=>new{a.Id,a.Name}))的实际代码;其中auditablePropertyMap看起来像这个公共静态表达式映射(Expression propertyMap),其中T:iaAuditable{Expression expr=(a=>new{a.CreatedBy,a.CreatedTime,a.UpdatedBy,a.UpdatedTime});//在这里如何组合这两个表达式的成员
public static class MapBuilder
{
public static Expression<Func<T, object>> GetMap<T>(Expression<Func<T, object>> func) where T: IAuditable
{
var body = func.Body as NewExpression;
var param = Expression.Parameter(typeof(T), "o");
var propertyAccessExprs = new List<Expression>();
foreach (MemberInfo member in body.Members)
{
propertyAccessExprs.Add(Expression.Property(param, member.Name));
}
var props = typeof(IAuditable).GetProperties();
foreach (PropertyInfo prop in props)
{
propertyAccessExprs.Add(Expression.Property(param, prop.Name));
}
var columnMappins = new List<Expression>();
foreach (var access in propertyAccessExprs)
{
columnMappins.Add(Expression.Call(typeof(EntityMap).GetMethod("Column", new Type[] {typeof(Object)}), Expression.Convert(access, typeof(Object))));
}
var RowExpr = Expression.Call(typeof(EntityMap).GetMethod("Row"), Expression.NewArrayInit(typeof(EntityMapColumn), columnMappins));
var result = Expression.Lambda<Func<T, object>>(RowExpr, param);
return result;
}
}
var builder = new ModelBuilder();
builder.Entity<SimpleAuditableObject>()
.HasKey(o => o.Id)
.MapSingleType(MapBuilder.GetMap<SimpleAuditableObject>(o => new { o.Id, o.Name }));
public interface IAuditable
{
int CreatedBy { get; set; }
DateTime CreatedTime { get; set; }
int UpdatedBy { get; set; }
DateTime UpdatedTime { get; set; }
}
public class SimpleAuditableObject : IAuditable
{
public int Id { get; set; }
public string Name { get; set; }
public int CreatedBy { get; set; }
public DateTime CreatedTime { get; set; }
public int UpdatedBy { get; set; }
public DateTime UpdatedTime { get; set; }
}