System.Linq.Dynamic.Core

repository·master·Indexed 23 days ago

https://github.com/zzzprojects/system.linq.dynamic.core

A .NET Core / Standard port of the Microsoft assembly for Dynamic LINQ functionality. It enables developers to write LINQ queries using string-based syntax on IQueryable objects via methods like .Where(), .OrderBy(), and .Select(). The library supports interpolated strings via WhereInterpolated on supported runtimes, custom type providers, and security configurations through ParsingConfig to manage method access and OrderBy restrictions.

Tokens
3.3K
Snippets
18
Records
24
Agent score
80%

What's inside System.Linq.Dynamic.Core

  1. Allow calling methods on custom classes using [DynamicLinqType]

    master

    By default, for security reasons, you can only call methods on standard predefined classes (like bool, int, string). To allow calling methods on your own custom classes within a dynamic LINQ expression, annotate the class with the [DynamicLinqType] attribute.

    [DynamicLinqType]
    public class MyCustomClass
    {
        public int GetAge(int x) => x;
    }
  2. Use Open Iconic's SVG Sprite

    master

    The SVG sprite allows you to load all icons in a single request. To use an icon, reference the sprite file and the specific icon ID within a <use> tag inside an <svg> element.

    Styling Tips:

    • Sizing: Set equal width and height on the <svg> tag.
    • Coloring: Use the CSS fill property on the <use> tag to change the icon color.
    • Best Practice: Add a general class to the <svg> tag for shared styles and a unique class to the <use> tag for icon-specific styling.
    <svg class="icon">
      <use xlink:href="open-iconic.svg#account-login" class="icon-account-login"></use>
    </svg>
    .icon {
      width: 16px;
      height: 16px;
    }
    
    .icon-account-login {
      fill: #f00;
    }
  3. Write Dynamic LINQ queries using string-based syntax

    master

    System.Linq.Dynamic.Core allows you to write LINQ queries using string expressions on an IQueryable. This is useful for building dynamic queries at runtime where the filter, sort, or projection criteria are provided as strings. You can use placeholders (e.g., @0, @1) to pass parameters into the query string to prevent injection and handle complex types safely.

    var query = db.Customers
        .Where("City == @0 and Orders.Count >= @1", "London", 10)
        .OrderBy("CompanyName")
        .Select("new(CompanyName as Name, Phone)");
  4. Implement Entity Framework Core Models and Contexts

    master

    Follow these steps to build your data access layer:

    1. Define Relationships: Build relationships between models (e.g., one-to-one, one-to-many, many-to-many) based on your database requirements.
    2. Configure Models: Use the Fluent API to configure models. Implement the IEntityTypeConfiguration<T> interface for each configuration.
    3. Create Base Context: Create a BaseContext class that inherits from DbContext and includes DbSet<T> properties for your models.
    4. Create Provider-Specific Contexts:
      • Create SqlServerContext inheriting from BaseContext. Override OnConfiguring and OnModelCreating.
      • Create PostgreSqlContext inheriting from BaseContext. Override OnConfiguring and OnModelCreating.
  5. Write Dynamic LINQ queries using strings

    master

    System.Linq.Dynamic.Core allows you to execute LINQ queries on an IQueryable using string-based expressions instead of strongly-typed lambda expressions. This is useful for building queries dynamically at runtime. You can use methods like .Where(), .OrderBy(), and .Select() with string arguments. To prevent injection and handle parameters safely, use placeholder syntax like @0, @1, etc., and pass the values as subsequent arguments to the method.

    var query = db.Customers
        .Where("City == @0 and Orders.Count >= @1", "London", 10)
        .OrderBy("CompanyName")
        .Select("new(CompanyName as Name, Phone)");
  6. Write Dynamic LINQ queries with string-based syntax

    master

    System.Linq.Dynamic.Core allows you to write LINQ queries using string-based syntax on an IQueryable. This is useful for building dynamic queries at runtime. You can use methods like .Where(), .OrderBy(), and .Select() with string arguments. Use @0, @1, etc., as placeholders for parameters to prevent injection and handle values correctly.

    var query = db.Customers
        .Where("City == @0 and Orders.Count >= @1", "London", 10)
        .OrderBy("CompanyName")
        .Select("new(CompanyName as Name, Phone)");