Доступ к методам Embodied для построения деревьев выражений - PullRequest
0 голосов
/ 27 февраля 2019

Попытка построить заказ по выражению, используя деревья выражений.Но я не могу получить доступ к свойству выражения выражения класса результата запроса.Это структура класса:

public class AssetFileRecord : IAuditable, IEntity, INavigateToCustomValues
{
    public AssetFileRecord()
    {
        this.UpdatedTimeStamp = DateTime.UtcNow;
    }

    public AssetFileRecord GetRecord()
    {
        return this;
    }

    public Guid Id { get; set; }
    public int DisplayId { get; set; }
    public string AssetTagNumber { get; set; }
    [JObjectIgnore]
    public virtual Account Account { get; set; }
    public string AccountNumber => Account?.AccountNumber;
    public string AuditTrail { get; set; }
    public string OldTagNumber { get; set; }
    public ActivityCode ActivityCode { get; set; }

    [JObjectIgnore]
    public virtual ICollection<AssetFileRecordDepreciation> AssetFileRecordDepreciations { get; set; }
    // Depreciation Records
    public double? AccumulatedDepreciation => Depreciation()?.AccumulatedDepreciation;
    public DateTime? DepreciationAsOfDate => Depreciation()?.DepreciationAsOfDate;
    public double? LifeMonths => Depreciation()?.LifeMonths;
    public double? DepreciationBasis => Depreciation()?.DepreciationBasis;
    public double? PeriodDepreciation => Depreciation()?.PeriodDepreciation;

    private AssetFileRecordDepreciation Depreciation()
    {
        return AssetFileRecordDepreciations?.AsQueryable()?.OrderBy(d => d.AssetFileDepreciationBook.BookNo)?.FirstOrDefault();
    }
}

Мне не удается добраться до свойства AccumulatedDepreciation, которое является свойством виртуального свойства AssetFileRecord.

Ниже приведен текущий код, который отлично работает длялюбые другие свойства без выражения.

    public static IQueryable<T> BuildOrderByExpression<T>(IQueryable<T> source, string sortProperty, ListSortDirection sortOrder, bool isFirstOrderTerm = true)
    {
        var type = typeof(T);
        var property = type.GetProperty(sortProperty, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
        var parameter = Expression.Parameter(type, "p");
        // ReSharper disable once AssignNullToNotNullAttribute
        Expression orderByExp;
        var map = AssetFileRecordExpressionHelper.GetExpressionEmbodiedMemberMappings();
        if (!map.TryGetValue($"{type.Name}.{property.Name}", out orderByExp))
        {
            var propertyAccess = Expression.MakeMemberAccess(parameter, property);
            orderByExp = Expression.Lambda(propertyAccess, parameter);
        }
        var typeArguments = new[] { type, property.PropertyType };
        var methodBase = isFirstOrderTerm ? "OrderBy" : "ThenBy";
        var methodName = sortOrder == ListSortDirection.Ascending ? methodBase : $"{methodBase}Descending";
        var resultExp = Expression.Call(typeof(Queryable), methodName, typeArguments, source.Expression, Expression.Quote(orderByExp));

        return source.Provider.CreateQuery<T>(resultExp);
    }

В приведенном выше коде "map" - это словарь, который возвращает соответствующие термины выражения для воплощенных элементов, за исключением тех, к которым обращаются в верхней части Depreciation (), и являетсяследующее.

public static Dictionary<string, Expression> GetExpressionEmbodiedMemberMappings()
        {
            var map = new Dictionary<string, Expression>
                {
                    {
                        $"{nameof(AssetFileRecord)}.{nameof(AssetFileRecord.AccountNumber)}",
                        (Expression<Func<AssetFileRecord, string>>) (
                            afr => afr.Account != null ? afr.Account.AccountNumber : null
                        )
                    }
                };
            return map;
        }

Expression.Call не оценивает допустимый запрос SQL, а скорее выдает исключение.

 ((System.Data.Entity.Infrastructure.DbQuery<AssetFileRecord>)records).Sql = '((System.Data.Entity.Infrastructure.DbQuery<AssetFileRecord>)records).Sql' threw an exception of type 'System.NotSupportedException'

Намеченный результат: он должен добавить порядок по выражению к сгенерированному дереву выраженийв конце;хотя это не удается сделать, при попытке заказа с помощью члена свойства с выражением bodied.

Может кто-нибудь, пожалуйста, помогите мне заставить это работать.

...