Enterprise Framework

Software Solutions in the Enterprise

.NET Linq OrderBy String Property Name

.NET Linq OrderBy and/or ThenBy using String Property Name Dynamically


Original Reference from:

https://stackoverflow.com/questions/36298868/how-to-dynamically-order-by-certain-entity-properties-in-entity-framework-7-cor


Linq Extensions

This extension extends IQueryable and IOrderedQueryable for dynamic conditions and string name sorting

using System;
using System.Linq;
using System.Linq.Expressions;

public static class IQueryableExtensions
{
    public static IQueryable<TSource> WhereIf<TSource>(
        this IQueryable<TSource> source,
        bool condition,
        Func<IQueryable<TSource>, IQueryable<TSource>> branch)
    {
        return condition ? branch(source) : source;
    }

    public static IQueryable<TSource> IncludeIf<TSource>(
        this IQueryable<TSource> source,
        bool condition,
        Func<IQueryable<TSource>, IQueryable<TSource>> branch)
    {
        return condition ? branch(source) : source;
    }

       public static IOrderedQueryable<T> OrderBy<T>(this         IQueryable<T> source, string propertyName, bool ascending = true)
    {
        if (ascending) return source.OrderBy(ToLambda<T>(propertyName));

        return source.OrderByDescending(ToLambda<T>(propertyName));
    }

        public static IOrderedQueryable<T>         OrderByDescending<T>(this IQueryable<T> source, string         propertyName, bool ascending = true)
    {
        if (ascending) return source.OrderBy(ToLambda<T>(propertyName));

        return source.OrderByDescending(ToLambda<T>(propertyName));
    }

        public static IOrderedQueryable<T> ThenBy<T>(this         IOrderedQueryable<T> source, string propertyName, bool ascending =         true)
    {
        if (ascending) return source.ThenBy(ToLambda<T>(propertyName));

        return source.ThenByDescending(ToLambda<T>(propertyName));
    }

        public static IOrderedQueryable<T> ThenByDescending<T>(this         IOrderedQueryable<T> source, string propertyName, bool ascending =         true)
    {
        if (ascending) return source.ThenBy(ToLambda<T>(propertyName));

        return source.ThenByDescending(ToLambda<T>(propertyName));
    }

    private static Expression<Func<T, object>> ToLambda<T>(string propertyName)
    {
        var parameter = Expression.Parameter(typeof(T));
        var property = Expression.Property(parameter, propertyName);

        return Expression.Lambda<Func<T, object>>(property, parameter);
    }
}


ASP.NET Core appsettings.json with AzureAD Properties

ASP.NET Core appsettings.json with AzureAD Properties


Here are appsettings.json properties for configuration of AzureAD in ASP.NET Core


{
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "yourtenantdomain.onmicrosoft.com",
    "ClientId": "{ClientId}",
    "TenantId": "common",
    "CallbackPath": "/signin-oidc", // defaults to /signin-oidc
    "ClientSecret": "{YourSecret}",

}



Additional Examples


https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2