TBARPT.Library
1.3.6
dotnet add package TBARPT.Library --version 1.3.6
NuGet\Install-Package TBARPT.Library -Version 1.3.6
<PackageReference Include="TBARPT.Library" Version="1.3.6" />
<PackageVersion Include="TBARPT.Library" Version="1.3.6" />
<PackageReference Include="TBARPT.Library" />
paket add TBARPT.Library --version 1.3.6
#r "nuget: TBARPT.Library, 1.3.6"
#:package TBARPT.Library@1.3.6
#addin nuget:?package=TBARPT.Library&version=1.3.6
#tool nuget:?package=TBARPT.Library&version=1.3.6
Introduction
The purpose of this library is to enable Dynamic LINQ string construction and execution, as well as dynamically creating and executing queries using expression trees. This is intended to support specific applications local to our environment, though it may be useful to others. Helpers are also available for retrieving lists of properties, their types, and display names from a given type. Using ReflectionIgnore attribute will prevent a class from returing results, or individual properties from being included in the list of properties.
Getting Started
- Add this package and dependencies
- Example usage:
using System.Reflection;
using TBARPT.Library;
using TBARPT.Library.Models;
using TBARPT.Repository.Repositories;
namespace TBARPT.Library.Example {
public class Example {
private readonly IReportRepository reportRepository;
public Example(IReportRepository reportRepository) {
this.reportRepository = reportRepository;
}
//when type is not known until runtime.
public QueryResult GetReport(QueryRequest queryRequest, string typeName) {
//use reflection to look up report source type
//it is recommended to have a base query with the same method name as the class name which holds properties for the report
Type type = Type.GetType("namespace.of.type." + typeName + ", assemblyName")
?? throw new ArgumentException($"Type could not be resolved: {typeName}");
//use reflection to find queryable method
//This should be wherever your base queries are stored
var queryMethod = reportRepository.GetType().GetMethod(typeName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)
?? throw new ArgumentException($"Invalid Report Source: {typeName}");
IQueryable<dynamic> baseQuery = (IQueryable<dynamic>?)queryMethod.Invoke(reportRepository, null)
?? throw new ArgumentException("Base query could not be resolved dynamically.");
var reportBuilder = Activator.CreateInstance(typeof(DynamicLINQReportBuilder<>).MakeGenericType(type))
?? throw new MissingMethodException("Could not create instance of DynamicLINQReportBuilder");
MethodInfo executeQuery = reportBuilder.GetType().GetMethod("GetReport")
?? throw new ArgumentException("GetReport method not found");
QueryResult result = (QueryResult?)executeQuery.Invoke(reportBuilder, [baseQuery, queryRequest])
?? throw new Exception("Execution of query returned null.");
return result;
}
//when type is known at compile time
public static QueryResult GetReport<T>(QueryRequest queryRequest, IQueryable<T> baseQuery) {
var reportBuilder = new DynamicLINQReportBuilder<T>();
QueryResult result = reportBuilder.GetReport((IQueryable<dynamic>)baseQuery, queryRequest);
return result;
}
//Utility to get list of properties from a type along with the display names (if set using DisplayName attribute) and types.
//If type is not known at compile time, use reflection to get the type.
public List<PropertyDetail> GetProperties(string className) {
Type type = Type.GetType("namespace.of.type." + className + ", assemblyName")
?? throw new ArgumentException($"Report source could not be resolved: {className}");
var helpers = Activator.CreateInstance(typeof(Helpers<>).MakeGenericType(type))
?? throw new MissingMethodException("Could not create instance of Helpers");
MethodInfo getProperties = helpers.GetType().GetMethod("GetProperties")
?? throw new ArgumentException("GetProperties method not found");
List<PropertyDetail> propertyDetails = (List<PropertyDetail>?)getProperties.Invoke(helpers, null) ?? [];
return propertyDetails;
}
//if the type is known at compile time
public List<PropertyDetail> GetProperties<T>(T someType) {
var helpers = new Helpers<T>();
List<PropertyDetail> propertyDetails = helpers.GetProperties();
return propertyDetails;
}
}
}
AutoMapper Example usage:
//map individual object
DestinationType destination = AutoMapper.MapTo<DestinationType>(sourceObject);
//map individual object with property name mapping
Dictionary<string, string> propertyMap = new Dictionary<string, string> {
{ "DestinationPropertyName", "SourcePropertyName" }
};
DestinationType destination = AutoMapper.MapTo<DestinationType>(sourceObject, propertyMap);
//map list of objects
List<DestinationType> destinationList = AutoMapper.MapToList<DestinationType>(sourceObjects);
//map list of objects with property name mapping
Dictionary<string, string> propertyMap = new Dictionary<string, string> {
{ "DestinationPropertyName", "SourcePropertyName" }
};
List<DestinationType> destinationList = AutoMapper.MapToList<DestinationType>(sourceObjects, propertyMap);
DynamicJoinEngine Example usage:
// Example usage demonstrating case-insensitive keys
public class Program
{
public static void Main()
{
// Create objects with differently cased property names
var departments = new List<object>
{
new { DepartmentId = 1, Name = "Engineering" },
new { DepartmentId = 2, Name = "Marketing" },
new { departmentId = 3, Name = "Finance" } // Note lowercase 'd' in departmentId
};
// Create ExpandoObjects with differently cased property names
var employees = new List<object>();
dynamic emp1 = new ExpandoObject();
emp1.EmployeeId = 101;
emp1.departmentId = 1; // Note lowercase 'd' in departmentId
emp1.Name = "John Doe";
employees.Add(emp1);
dynamic emp2 = new ExpandoObject();
emp2.employeeId = 102; // Note lowercase 'e' in employeeId
emp2.DEPARTMENTID = 1; // Note uppercase 'DEPARTMENTID'
emp2.Name = "Jane Smith";
employees.Add(emp2);
dynamic emp3 = new ExpandoObject();
emp3.EmployeeId = 103;
emp3.DepartmentId = 2;
emp3.Name = "Bob Johnson";
employees.Add(emp3);
// Example with composite keys of differently cased property names
var projects = new List<object>();
dynamic proj1 = new ExpandoObject();
proj1.ProjectID = "P1"; // Note uppercase 'ID'
proj1.DepartmentId = 1;
proj1.ProjectName = "Website Redesign";
projects.Add(proj1);
dynamic proj2 = new ExpandoObject();
proj2.projectId = "P2"; // Note lowercase 'projectId'
proj2.DEPARTMENTID = 1; // Note uppercase 'DEPARTMENTID'
proj2.ProjectName = "Mobile App";
projects.Add(proj2);
// Initialize the dynamic join engine with case-insensitive keys (default)
var joinEngine = new DynamicJoinFramework.DynamicJoinEngine()
.AddTable("employees", employees)
.AddTable("departments", departments)
.AddJoin("employees", "departments", "departmentId", "DepartmentId",
DynamicJoinFramework.JoinType.Left);
var results = joinEngine.Execute();
Console.WriteLine("Case-insensitive join results:");
foreach (var item in results)
{
Console.WriteLine("---");
if (item is IDictionary<string, object> expandoItem)
{
foreach (var kvp in expandoItem)
{
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
}
}
// Composite key join with case-insensitive keys
var compositeJoinEngine = new DynamicJoinFramework.DynamicJoinEngine()
.AddTable("employees", employees)
.AddTable("projects", projects)
.AddJoin("employees", "projects", "departmentId", "DEPARTMENTID",
DynamicJoinFramework.JoinType.Inner);
var compositeResults = compositeJoinEngine.Execute();
Console.WriteLine("\nComposite key case-insensitive join results:");
foreach (var item in compositeResults)
{
Console.WriteLine("---");
if (item is IDictionary<string, object> expandoItem)
{
foreach (var kvp in expandoItem)
{
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
}
}
// Optional: Create a case-sensitive engine for comparison
var caseSensitiveEngine = new DynamicJoinFramework.DynamicJoinEngine(caseInsensitiveKeys: false)
.AddTable("employees", employees)
.AddTable("departments", departments)
.AddJoin("employees", "departments", "departmentId", "DepartmentId",
DynamicJoinFramework.JoinType.Left);
var sensitiveResults = caseSensitiveEngine.Execute();
Console.WriteLine("\nCase-sensitive join results (for comparison):");
Console.WriteLine($"Number of results: {sensitiveResults.Count()}"); // Should be fewer matches
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Newtonsoft.Json (>= 13.0.3)
- System.Linq.Dynamic.Core (>= 1.6.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated | |
---|---|---|---|
1.3.6 | 289 | 9/16/2025 | |
1.3.5 | 120 | 9/5/2025 | |
1.3.4 | 168 | 9/4/2025 | |
1.3.3.1-beta | 253 | 9/4/2025 | |
1.3.3 | 156 | 9/4/2025 | |
1.3.3-beta | 261 | 9/3/2025 | |
1.3.2 | 109 | 8/22/2025 | |
1.3.1 | 154 | 8/20/2025 | |
1.3.0 | 158 | 8/13/2025 | |
1.2.1-beta | 153 | 8/13/2025 | |
1.2.0-beta | 158 | 8/11/2025 | |
1.1.3 | 347 | 6/10/2025 | |
1.1.1 | 139 | 4/5/2025 | |
1.1.0 | 200 | 4/1/2025 | |
1.0.4 | 489 | 3/25/2025 | |
1.0.3 | 156 | 3/17/2025 | |
1.0.2 | 203 | 3/11/2025 | |
1.0.1 | 224 | 3/7/2025 | |
1.0.0 | 231 | 3/7/2025 |
Initial release.