Ice Breaking

Unlocking Enhanced Data Querying Capabilities with DynamicWhere.ex for JSON Field Filtering and Ordering

Important Considerations When Filtering or Ordering Based on JSON Fields in Your Database:

  1. Target .NET 7 or Compatible Versions: Ensure that your application is targeting .NET 7 or a compatible version to leverage the full potential of DynamicWhere.ex for handling JSON fields.

  2. Correct Database Mapping: Confirm that the JSON field in your database is accurately mapped to your application model, ensuring seamless interaction between your application and the underlying data.

Example Scenario:

Let's delve into a practical example to illustrate these concepts:

// Define a Model for Your Data
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    
    // Object Property Will Be Stored as a JSON Field in the Database
    public Properties Properties { get; set; } 
}

// Define a Class for JSON Object Properties
public class Properties
{
    public string Color { get; set; }
    public string Size { get; set; }
}

Filtering Based on JSON Objects:

// Define a Filter Condition on a JSON Object Property
Condition condition = new Condition
{
    Field = "Properties.Color", 
    DataType = DataType.Text,
    Operator = Operator.Equal,
    Values = new List<string> { "Red" }
};

// Apply the Filter
query = dbContext.Products.Where(condition);

In this example, we are filtering products by the "Color" property within the Properties JSON field.

Ordering Based on JSON Objects:

// Create an Order-By Configuration
OrderBy orderBy = new OrderBy
{
    Field = "Properties.Size", 
    Direction = Direction.Ascending
};

// Apply the Ordering
query = dbContext.Products.Order(orderBy);

Here, we are ordering products by the "Size" property within the Properties JSON field.

DynamicWhere.ex simplifies the process of working with nested JSON objects, offering flexibility and efficiency in your data queries.

Last updated