RSCG – rscg_queryables

RSCG – rscg_queryables
 
 

name rscg_queryables
nuget https://www.nuget.org/packages/rscg_queryables/
https://www.nuget.org/packages/rscg_queryablesCommon/
link https://github.com/ignatandrei/rscg_queryables
author Andrei Ignat

Generating code for .Where and .OrderBy by string, not by lambda

 

This is how you can use rscg_queryables .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<ItemGroup>
		<PackageReference  Include="rscg_queryablesCommon" Version="2024.1110.1815" />
		<PackageReference Include="rscg_queryables" Version="2024.1110.1815"  OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
	</ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


using SortAndWhere;


Console.WriteLine("Hello, World!");

var students = new Student[]
{
    new Student { FirstName = "John", LastName = "Doe", StartYear = 1935},
    new Student { FirstName = "Ignat", LastName = "Andrei", StartYear = 1989 },
};

var orderedExplicitly = students.OrderBy(p => p.FirstName).ToArray();
var orderedImplicitly = students.OrderBy("firStnaMe").ToArray();
var orderedImplicitly2 = students.AsQueryable().OrderBy("fIrsTnAme").ToArray();


//Search by property name

var search = students.AsQueryable().Where("firstName", rscg_queryablesCommon.WhereOperator.Equal, "John").ToArray();

Console.WriteLine("found : " + search.Length);

search = students.AsQueryable().Where(Student_.Where_Expr("firstName", rscg_queryablesCommon.WhereOperator.Equal, "John")).ToArray();
Console.WriteLine("found : " + search.Length);





using rscg_queryablesCommon;

namespace SortAndWhere;
[MakeSortable]
[MakeWhere]
public class Student
{
    public string FirstName { get; set; } = string.Empty;
    public string LastName { get; set; } = string.Empty;

    public int StartYear { get; set; }

}


 

The code that is generated is


public static partial class ExtensionsSortable_SortAndWhere_Student {
    
    #region Enumerable
    public static System.Linq.IOrderedEnumerable<global::SortAndWhere.Student> OrderBy
    (
    this IEnumerable<global::SortAndWhere.Student>
        source, string propertyName
    )
    {

        return OrderByAscDesc(source,propertyName,true);
    }
        public static System.Linq.IOrderedEnumerable<global::SortAndWhere.Student>
            OrderByDescending
            (
            this IEnumerable<global::SortAndWhere.Student>
                source, string propertyName
                )
                {

                return OrderByAscDesc(source,propertyName,false);
                }
    public static System.Linq.IOrderedEnumerable<global::SortAndWhere.Student> OrderByAscDesc
    (
     this IEnumerable<global::SortAndWhere.Student> source, string propertyName, bool Ascending
    )
   {

                        if(string.Equals(propertyName, "FirstName", StringComparison.OrdinalIgnoreCase)){
                            if(Ascending)
                                return source.OrderBy(x => x.FirstName);
                            else
                                return source.OrderByDescending(x => x.FirstName);                            
                        }
                    
                        if(string.Equals(propertyName, "LastName", StringComparison.OrdinalIgnoreCase)){
                            if(Ascending)
                                return source.OrderBy(x => x.LastName);
                            else
                                return source.OrderByDescending(x => x.LastName);                            
                        }
                    
                        if(string.Equals(propertyName, "StartYear", StringComparison.OrdinalIgnoreCase)){
                            if(Ascending)
                                return source.OrderBy(x => x.StartYear);
                            else
                                return source.OrderByDescending(x => x.StartYear);                            
                        }
                                    throw new ArgumentException($"Property {propertyName} not found", propertyName);
    }

    public static System.Linq.IOrderedEnumerable<global::SortAndWhere.Student> ThenByAscDesc
    (
     this IOrderedEnumerable<global::SortAndWhere.Student> source, string propertyName, bool Ascending
    )
   {

                        if(string.Equals(propertyName, "FirstName", StringComparison.OrdinalIgnoreCase)){
                            if(Ascending)
                                return source.ThenBy(x => x.FirstName);
                            else
                                return source.ThenByDescending(x => x.FirstName);                            
                        }
                    
                        if(string.Equals(propertyName, "LastName", StringComparison.OrdinalIgnoreCase)){
                            if(Ascending)
                                return source.ThenBy(x => x.LastName);
                            else
                                return source.ThenByDescending(x => x.LastName);                            
                        }
                    
                        if(string.Equals(propertyName, "StartYear", StringComparison.OrdinalIgnoreCase)){
                            if(Ascending)
                                return source.ThenBy(x => x.StartYear);
                            else
                                return source.ThenByDescending(x => x.StartYear);                            
                        }
                                    throw new ArgumentException($"Property {propertyName} not found", propertyName);
    }
    public static System.Linq.IOrderedEnumerable<global::SortAndWhere.Student> ThenBy
    (
    this IOrderedEnumerable<global::SortAndWhere.Student>
        source, string propertyName
    )
    {

        return ThenByAscDesc(source,propertyName,true);
    }
    public static System.Linq.IOrderedEnumerable<global::SortAndWhere.Student> ThenByDescending
    (
    this IOrderedEnumerable<global::SortAndWhere.Student>
        source, string propertyName
    )
    {

        return ThenByAscDesc(source,propertyName,false);
    }

    #endregion 

#region Queryable
    public static System.Linq.IOrderedQueryable<global::SortAndWhere.Student> OrderBy
    (
    this IQueryable<global::SortAndWhere.Student>
        source, string propertyName
    )
    {

        return OrderByAscDesc(source,propertyName,true);
    }
        public static System.Linq.IOrderedQueryable<global::SortAndWhere.Student>
            OrderByDescending
            (
            this IQueryable<global::SortAndWhere.Student>
                source, string propertyName
                )
                {

                return OrderByAscDesc(source,propertyName,false);
                }
    public static System.Linq.IOrderedQueryable<global::SortAndWhere.Student> OrderByAscDesc
    (
     this IQueryable<global::SortAndWhere.Student> source, string propertyName, bool Ascending
    )
   {

                        if(string.Equals(propertyName, "FirstName", StringComparison.OrdinalIgnoreCase)){
                            if(Ascending)
                                return source.OrderBy(x => x.FirstName);
                            else
                                return source.OrderByDescending(x => x.FirstName);                            
                        }
                    
                        if(string.Equals(propertyName, "LastName", StringComparison.OrdinalIgnoreCase)){
                            if(Ascending)
                                return source.OrderBy(x => x.LastName);
                            else
                                return source.OrderByDescending(x => x.LastName);                            
                        }
                    
                        if(string.Equals(propertyName, "StartYear", StringComparison.OrdinalIgnoreCase)){
                            if(Ascending)
                                return source.OrderBy(x => x.StartYear);
                            else
                                return source.OrderByDescending(x => x.StartYear);                            
                        }
                                    throw new ArgumentException($"Property {propertyName} not found", propertyName);
    }
    public static System.Linq.IOrderedQueryable<global::SortAndWhere.Student> ThenByAscDesc
    (
     this IOrderedQueryable<global::SortAndWhere.Student> source, string propertyName, bool Ascending
    )
   {

                        if(string.Equals(propertyName, "FirstName", StringComparison.OrdinalIgnoreCase)){
                            if(Ascending)
                                return source.ThenBy(x => x.FirstName);
                            else
                                return source.ThenByDescending(x => x.FirstName);                            
                        }
                    
                        if(string.Equals(propertyName, "LastName", StringComparison.OrdinalIgnoreCase)){
                            if(Ascending)
                                return source.ThenBy(x => x.LastName);
                            else
                                return source.ThenByDescending(x => x.LastName);                            
                        }
                    
                        if(string.Equals(propertyName, "StartYear", StringComparison.OrdinalIgnoreCase)){
                            if(Ascending)
                                return source.ThenBy(x => x.StartYear);
                            else
                                return source.ThenByDescending(x => x.StartYear);                            
                        }
                                    throw new ArgumentException($"Property {propertyName} not found", propertyName);
    }
    public static System.Linq.IOrderedQueryable<global::SortAndWhere.Student> ThenBy
    (
    this IOrderedQueryable<global::SortAndWhere.Student>
        source, string propertyName
    )
    {

        return ThenByAscDesc(source,propertyName,true);
    }
    public static System.Linq.IOrderedQueryable<global::SortAndWhere.Student> ThenByDescending
    (
    this IOrderedQueryable<global::SortAndWhere.Student>
        source, string propertyName
    )
    {

        return ThenByAscDesc(source,propertyName,false);
    }

    #endregion 

}

public static class ExtensionsWhere_SortAndWhere_Student{
    

        internal static System.Linq.Expressions.Expression < Func < global::SortAndWhere.Student,bool>>
        FirstName_Expr_Equal(string argument) => (a => a.FirstName== argument);

        internal static Func<global::SortAndWhere.Student,bool>
            FirstName_Equal(string argument) => (a => a.FirstName== argument);

        internal static System.Linq.Expressions.Expression < Func < global::SortAndWhere.Student,bool>>
        FirstName_Expr_NotEqual(string argument) => (a => a.FirstName != argument);

        internal static Func<global::SortAndWhere.Student,bool>
                FirstName_NotEqual(string argument) => (a => a.FirstName != argument);

    
        internal static System.Linq.Expressions.Expression < Func < global::SortAndWhere.Student,bool>>
        LastName_Expr_Equal(string argument) => (a => a.LastName== argument);

        internal static Func<global::SortAndWhere.Student,bool>
            LastName_Equal(string argument) => (a => a.LastName== argument);

        internal static System.Linq.Expressions.Expression < Func < global::SortAndWhere.Student,bool>>
        LastName_Expr_NotEqual(string argument) => (a => a.LastName != argument);

        internal static Func<global::SortAndWhere.Student,bool>
                LastName_NotEqual(string argument) => (a => a.LastName != argument);

    
        internal static System.Linq.Expressions.Expression < Func < global::SortAndWhere.Student,bool>>
        StartYear_Expr_Equal(int argument) => (a => a.StartYear== argument);

        internal static Func<global::SortAndWhere.Student,bool>
            StartYear_Equal(int argument) => (a => a.StartYear== argument);

        internal static System.Linq.Expressions.Expression < Func < global::SortAndWhere.Student,bool>>
        StartYear_Expr_NotEqual(int argument) => (a => a.StartYear != argument);

        internal static Func<global::SortAndWhere.Student,bool>
                StartYear_NotEqual(int argument) => (a => a.StartYear != argument);

    
   

}
public static class Student_{


        public static System.Linq.IQueryable<global::SortAndWhere.Student>
            Where(this System.Linq.IQueryable<global::SortAndWhere.Student> original,
                string propertyName,rscg_queryablesCommon.WhereOperator operatorWhere, string argument)
        {
            return original.Where(Where_Expr(propertyName, operatorWhere, argument));
        }
        public static System.Linq.Expressions.Expression < Func < global::SortAndWhere.Student,bool>>
        Where_Expr(string propertyName,rscg_queryablesCommon.WhereOperator operatorWhere, string argument)
            {

                    if(string.Equals(propertyName,  "FirstName", StringComparison.OrdinalIgnoreCase)){
                    switch(operatorWhere){
                    case rscg_queryablesCommon.WhereOperator.Equal:
                    return ExtensionsWhere_SortAndWhere_Student.FirstName_Expr_Equal( argument);
                    case rscg_queryablesCommon.WhereOperator.NotEqual:
                    return ExtensionsWhere_SortAndWhere_Student.FirstName_Expr_NotEqual( argument);
                    default:
                    throw new ArgumentException($"Operator {operatorWhere} not found");
                    }//end switch
                    }//end if FirstName

                
                    if(string.Equals(propertyName,  "LastName", StringComparison.OrdinalIgnoreCase)){
                    switch(operatorWhere){
                    case rscg_queryablesCommon.WhereOperator.Equal:
                    return ExtensionsWhere_SortAndWhere_Student.LastName_Expr_Equal( argument);
                    case rscg_queryablesCommon.WhereOperator.NotEqual:
                    return ExtensionsWhere_SortAndWhere_Student.LastName_Expr_NotEqual( argument);
                    default:
                    throw new ArgumentException($"Operator {operatorWhere} not found");
                    }//end switch
                    }//end if LastName

                
            throw new ArgumentException("Property "+ propertyName +" not found for string type");
            }





        public static Func<global::SortAndWhere.Student,bool>
            Where(string propertyName,rscg_queryablesCommon.WhereOperator operatorWhere, string argument)
            {

                    if(string.Equals(propertyName,  "FirstName", StringComparison.OrdinalIgnoreCase)){
                    switch(operatorWhere){
                    case rscg_queryablesCommon.WhereOperator.Equal:
                    return ExtensionsWhere_SortAndWhere_Student.FirstName_Equal( argument);
                    case rscg_queryablesCommon.WhereOperator.NotEqual:
                    return ExtensionsWhere_SortAndWhere_Student.FirstName_NotEqual( argument);
                    default:
                    throw new ArgumentException($"Operator {operatorWhere} not found");
                    }//end switch
                    }//end if FirstName

                
                    if(string.Equals(propertyName,  "LastName", StringComparison.OrdinalIgnoreCase)){
                    switch(operatorWhere){
                    case rscg_queryablesCommon.WhereOperator.Equal:
                    return ExtensionsWhere_SortAndWhere_Student.LastName_Equal( argument);
                    case rscg_queryablesCommon.WhereOperator.NotEqual:
                    return ExtensionsWhere_SortAndWhere_Student.LastName_NotEqual( argument);
                    default:
                    throw new ArgumentException($"Operator {operatorWhere} not found");
                    }//end switch
                    }//end if LastName

                
            throw new ArgumentException("Property "+ propertyName +" not found for string type");
            }

    
        public static System.Linq.IQueryable<global::SortAndWhere.Student>
            Where(this System.Linq.IQueryable<global::SortAndWhere.Student> original,
                string propertyName,rscg_queryablesCommon.WhereOperator operatorWhere, int argument)
        {
            return original.Where(Where_Expr(propertyName, operatorWhere, argument));
        }
        public static System.Linq.Expressions.Expression < Func < global::SortAndWhere.Student,bool>>
        Where_Expr(string propertyName,rscg_queryablesCommon.WhereOperator operatorWhere, int argument)
            {

                    if(string.Equals(propertyName,  "StartYear", StringComparison.OrdinalIgnoreCase)){
                    switch(operatorWhere){
                    case rscg_queryablesCommon.WhereOperator.Equal:
                    return ExtensionsWhere_SortAndWhere_Student.StartYear_Expr_Equal( argument);
                    case rscg_queryablesCommon.WhereOperator.NotEqual:
                    return ExtensionsWhere_SortAndWhere_Student.StartYear_Expr_NotEqual( argument);
                    default:
                    throw new ArgumentException($"Operator {operatorWhere} not found");
                    }//end switch
                    }//end if StartYear

                
            throw new ArgumentException("Property "+ propertyName +" not found for int type");
            }





        public static Func<global::SortAndWhere.Student,bool>
            Where(string propertyName,rscg_queryablesCommon.WhereOperator operatorWhere, int argument)
            {

                    if(string.Equals(propertyName,  "StartYear", StringComparison.OrdinalIgnoreCase)){
                    switch(operatorWhere){
                    case rscg_queryablesCommon.WhereOperator.Equal:
                    return ExtensionsWhere_SortAndWhere_Student.StartYear_Equal( argument);
                    case rscg_queryablesCommon.WhereOperator.NotEqual:
                    return ExtensionsWhere_SortAndWhere_Student.StartYear_NotEqual( argument);
                    default:
                    throw new ArgumentException($"Operator {operatorWhere} not found");
                    }//end switch
                    }//end if StartYear

                
            throw new ArgumentException("Property "+ propertyName +" not found for int type");
            }

    

}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg_queryables