- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在转换 Expression<T, bool>
到Expression<Y, bool>
其中 T 和 Y 是不同的实体,除了通过 Automapper 映射之外,没有任何关联。本质上,我有一个我的代码使用的模型对象:
public class Store
{
public string StoreId { get; set; }
public string Name { get; set; }
public List<Phone> Phones { get; set; }
public Address Address { get; set; }
public Account Account { get; set; }
public Status Status { get; set; }
}
我正在映射到一个实体对象以存储在我的 mongo 数据库中:
public class Store : MongoEntity
{
public string AccountId { get; set; }
public string Name { get; set; }
public List<string> UserIds { get; set; }
public List<Phone> PhoneNumbers { get; set; }
public Address Address { get; set; }
}
public abstract class MongoEntity : IMongoEntity
{
[BsonId]
public ObjectId Id { get; set; }
public Status Status { get; set; }
}
我使用这个问题的答案来计算如何在表达式 ( Question ) 之间进行转换,这使我达到了 90%。我能够修改代码以获取模型存储和实体存储之间的 AutoMapper 映射,并从源属性中获取目标属性:
private Expression<Func<TNewTarget, bool>> TransformPredicateLambda<TOldTarget, TNewTarget>(
Expression<Func<TOldTarget, bool>> predicate)
{
var lambda = (LambdaExpression)predicate;
if (lambda == null)
{
throw new NotSupportedException();
}
//Modified here to get automapper mappings
var maps = Mapper.FindTypeMapFor<TOldTarget, TNewTarget>();
var mutator = new ExpressionTargetTypeMutator(t => typeof(TNewTarget), maps);
var explorer = new ExpressionTreeExplorer();
var converted = mutator.Visit(predicate.Body);
return Expression.Lambda<Func<TNewTarget, bool>>(
converted,
lambda.Name,
lambda.TailCall,
explorer.Explore(converted).OfType<ParameterExpression>());
}
protected override Expression VisitMember(MemberExpression node)
{
var dataContractType = node.Member.ReflectedType;
var activeRecordType = _typeConverter(dataContractType);
PropertyMap prop = null;
foreach (var propertyMap in _maps)
{
var source = propertyMap.SourceMember;
var dest = propertyMap.DestinationProperty;
if (source != null && source.Name == node.Member.Name)
{
prop = propertyMap;
}
}
if (prop == null)
{
return base.VisitMember(node);
}
var propertyName = prop.DestinationProperty.Name;
var property = activeRecordType.GetProperty(propertyName);
var converted = Expression.MakeMemberAccess(
base.Visit(node.Expression),
property
);
return converted;
}
问题是,我的实体对象并不具有与模型对象相同的所有属性(例如,Account 对象与 AccountId)。当 Transformer 到达 Model 对象的 Account 属性时,我得到一个异常(因为我的 Entity 对象上没有匹配的属性)。我不能从 VisitMember 返回 null,也不允许使用 new Expression()。我如何处理忽略我的模型对象上不存在于我的实体对象上的属性?
使用评论中的信息更新
所以,为了更清楚一点,我正在使用 Automapper 从 Models.Store 映射到 Entity.Store。我的 entity.Store 只有一个 AccountId(因为我不想复制所有帐户数据),但我的 Models.Store 需要整个帐户对象(我可以通过查询帐户集合获得)。
Automapper 基本上将我的 Account 对象转换为我实体上的一个 AccountId。因此,当我搜索 x => x.Account.AccountId == abcd1234
时(其中 x 是 models.Store),我需要将表达式转换为 x => x.AccountId == abcd1234
(其中 x 是 Entity.Store)。
我有那部分工作(将 mS => mS.Account.AccountId == 1234
更改为 mE => mE.AccountId == 1234
)。我现在遇到的问题是,在执行 AccountId 属性后,以 Account 作为节点调用 VisitMember。由于我的 Entity.Store 对象中没有帐户,因此出现异常。
最佳答案
如果没有可测试/可运行的代码,就很难测试解决方案。但这是一个猜测
给定以下表达式 mS => mS.Account.AccountId == 1234
并希望转换 MemberExpressions
,您将收到以下调用:
VisitMember(mS.Account.AccountId
VisitMember(mS.Account)
您想将第二个转换为 mE.AccountId
。这涉及两个转换:一是将属性访问从 (EntityType).(AccountType).AccountId
更改为 (MongoStoreType).AccountId
,同时更改底层对象。如果您已经在 ExpressionVisitor
的其他方法(可能是 VisitParameter
和 VisitLambda
)中处理参数转换,那您就没问题了。然后,您只需跳过查看父级 MemberAccess
,直接跳转到祖父级:
var converted = Expression.MakeMemberAccess(
base.Visit(node.Expression),
property
);
return converted;
变成这样:
var parentMember = node.Expression as MemberExpression;
if (parentMember != null)
{
var grandparent = parentMember.Expression;
var converted = Expression.MakeMemberAccess(
base.Visit(grandparent),
property
);
return converted;
}
else
{
var converted = Expression.MakeMemberAccess(
base.Visit(node.Expression),
property
);
return converted;
}
关于c# - 表达式树 Null VisitMember,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386978/
最近开始学习oracle和sql。 在学习的过程中,我遇到了几个问题,我的 friend 在接受采访时被问到这些问题。 SELECT * FROM Employees WHERE NULL IS N
这个问题在这里已经有了答案: Can we subtract NULL pointers? (4 个回答) 关闭 2 个月前。 是否定义了NULL - NULL? (char *)NULL - (ch
是否有推荐的方法(根据 .net Framework 指南)检查 null,例如: if (value == null) {//code1} else {//code2} 或 if (value !=
我正在尝试将值插入数据库,但出现这样的错误任何人都可以告诉我为什么该值为空,如下所示: An exception occurred while executing 'INSERT INTO perso
这个问题在这里已经有了答案: String concatenation with a null seems to nullify the entire string - is that desire
您好,我正在 Android 联系人搜索模块中工作。我正在查询下方运行。 cur = context.getContentResolver().query(ContactsContract.Data.
下面的 SQL 表定义说明了从我的 MYSQL 数据库创建表的语句之一,该数据库是由我公司的前开发人员开发的。 DROP TABLE IF EXISTS `classifieds`.`category
我主要有应用程序开发背景。在编程语言中 variable == null或 variable != null有效。 当涉及到 SQL 时,以下查询不会给出任何语法错误,但也不会返回正确的结果。 sel
我在尝试检查某些元素是否为 NULL 时遇到段错误或不。任何人都可以帮忙吗? void addEdge(int i, int j) { if (i >= 0 && j > 0)
在 SQL 服务器中考虑到以下事实:Col1 和 Col2 包含数值和 NULL 值 SELECT COALESCE(Col1,Col2) 返回一个错误:“COALESCE 的至少一个参数必须是一个不
在 SQL 服务器中考虑到以下事实:Col1 和 Col2 包含数值和 NULL 值 SELECT COALESCE(Col1,Col2) 返回一个错误:“COALESCE 的至少一个参数必须是一个不
下面查询的关系代数表达式是什么?我找不到“Is Null”的表达式。 SELECT reader.name FROM reader LEFT JOIN book_borrow ON reader.ca
我正在尝试使用三元运算符来检查值是否为 null 并返回一个表达式或另一个。将此合并到 LINQ 表达式时,我遇到的是 LINQ 表达式的 Transact-SQL 转换试图执行“column = n
我在给定的代码中看到了以下行: select(0, (fd_set *) NULL, (fd_set *) NULL, (fd_set *) NULL, &timeout); http://linux
var re = /null/g; re.test('null null'); //> true re.test('null null'); //> true re.test('null null')
这个问题在这里已经有了答案: 关闭 13 年前。 我今天避开了一场关于数据库中空值的激烈辩论。 我的观点是 null 是未指定值的极好指示符。团队中有意见的其他每个人都认为零和空字符串是可行的方法。
由于此错误,我无法在模拟器中运行我的应用: Error:null value in entry: streamOutputFolder=null 或 gradle - Error:null value
我正在尝试在 Android 应用程序中创建电影数据库,但它返回错误。知道这意味着什么吗? public Cursor returnData() { return db.query(TABLE
我一直在检查浏览器中的日期函数以及运行时间 new Date (null, null, null); 在开发工具控制台中,它给出了有效的日期 Chrome v 61 回归 Sun Dec 31 189
为什么 NA==NULL 会导致 logical (0) 而不是 FALSE? 为什么 NULL==NULL 会导致 logical(0) 而不是 TRUE? 最佳答案 NULL 是一个“零长度”对象
我是一名优秀的程序员,十分优秀!