配列に渡された値の順序に基づいて並べ替える式を作成しようとしています。私はほとんどの概念を理解していませんが、どのように式を選択して3値を連鎖させることができるかについて混乱しています。
/// <summary>
/// This method will build an expression to map the order
/// of the ownerkeys to it's index to use for sorting
/// I.E I have an array [DivisionOwnerGuid, CompanyOwnerGuid]
/// ownerKey == {DivisionOwnerGuid} ? 0
/// ownerKey == {CompanyOwnerGuid} ? 1 : Int32.MaxValue
/// </summary>
protected Expression<Func<IConfigurationValue, int>> GetOrderByExpression(
guid[] ownerKeyByPriority)
{
// x =>
var parameter = Expression.Parameter(typeof(IConfigurationValue));
ownerKeyByPriority.Select((ownerKey, index) => new {
// Hack: Build the expressions using the same parameter
// to avoid having to use Expression Visitor
Condition = this.GetCondition(parameter, ownerKey),
Priority = index
}).Aggregate((accumulated, nextPriorityExpression) => {
// How can I aggregate the conditions together
Expression.Condition(nextPriorityExpression, nextPriorityExpression.Priority, )
});
}
protected BinaryExpression GetCondition(ParameterExpression parameter, guid ownerKey)
{
// x => x.OwnerGlobalIdRowKey
var property = Expression.Property(parameter,
nameof(IConfigurationValue.OwnerGlobalIdRowKey));
// x => x.OwnerGlobalIdRowKey == { valueOf(ownerKey) }
return Expression.Equal(property, Expression.Constant(ownerKey,
typeof(Guid)));
}
集計関数を介しての三元チェーンを伝播し続けるにはどうすればよいですか?
の戻り値の(潜在的な)型エラーを無視するGetCondition
と、ネストされた3項演算子の本体Aggregate
を適切な型で累積して、ラムダを作成できます。
Expression<Func<IConfigurationValue, int>> GetOrderByExpression(
Guid[] ownerKeyByPriority) {
// x =>
var parameter = Expression.Parameter(typeof(IConfigurationValue));
var body = ownerKeyByPriority.Select((ownerKey, index) => new {
// Hack: Build the expressions using the same parameter
// to avoid having to use Expression Visitor
Test = this.GetCondition(parameter, ownerKey),
Priority = Expression.Constant(index)
}).Aggregate((Expression)Expression.Constant(int.MaxValue), (accumulated, nextPriorityExpression) =>
Expression.Condition(nextPriorityExpression.Test, nextPriorityExpression.Priority, accumulated)
);
return (Expression<Func<IConfigurationValue, int>>)Expression.Lambda(body, parameter);
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加