< Summary

Information
Class: MyNet.UI.ViewModels.List.Filtering.Filters.SelectedValuesFilterViewModel<T1, T2>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/SelectedValuesFilterViewModel.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 20
Coverable lines: 20
Total lines: 87
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
set_Operator(...)100%210%
set_Values(...)100%210%
set_AvailableValues(...)100%210%
get_IsEmpty()0%620%
BuildExpressionCore()0%4260%
Reset()100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/SelectedValuesFilterViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SelectedValuesFilterViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Linq.Expressions;
 10using MyNet.Primitives;
 11
 12namespace MyNet.UI.ViewModels.List.Filtering.Filters;
 13
 14/// <summary>
 15/// Filter view model for selecting multiple values from a list of available values.
 16/// Matches items where the property value is contained in the selected values list.
 17/// Supports Is/IsNot operators.
 18/// </summary>
 19/// <typeparam name="T">The type of the items being filtered.</typeparam>
 20/// <typeparam name="TValue">The type of the property value to filter on.</typeparam>
 21/// <param name="propertyName">The key that identifies this filter condition.</param>
 22/// <param name="property">The expression representing the property to filter on.</param>
 23/// <param name="availableValues">The list of available values for selection.</param>
 24/// <param name="operatorMode">The binary operator to use (default: Is).</param>
 25public class SelectedValuesFilterViewModel<T, TValue>(
 26    string propertyName,
 27    Expression<Func<T, TValue?>> property,
 28    IEnumerable<TValue> availableValues,
 29    BinaryOperator operatorMode = BinaryOperator.Is)
 030    : FilterConditionViewModel<T>(propertyName)
 31{
 32    /// <summary>
 33    /// Gets the expression representing the property to filter on.
 34    /// </summary>
 035    public Expression<Func<T, TValue?>> Property { get; } = property;
 36
 37    /// <summary>
 38    /// Gets or sets the binary operator used for matching (Is or IsNot).
 39    /// </summary>
 040    public BinaryOperator Operator { get; set => SetProperty(ref field, value); } = operatorMode;
 41
 42    /// <summary>
 43    /// Gets or sets the selected values to match against.
 44    /// When null or empty, the filter is considered empty.
 45    /// </summary>
 046    public TValue[]? Values { get; set => SetProperty(ref field, value); }
 47
 48    /// <summary>
 49    /// Gets or sets the available values for selection.
 50    /// </summary>
 051    public IEnumerable<TValue> AvailableValues { get; set => SetProperty(ref field, value); } = availableValues;
 52
 53    /// <summary>
 54    /// Gets a value indicating whether this filter is in an empty state.
 55    /// A selected values filter is empty when <see cref="Values"/> is null or empty.
 56    /// </summary>
 057    public override bool IsEmpty => Values is null || Values.Length == 0;
 58
 59    /// <inheritdoc />
 60    protected override Expression<Func<T, bool>> BuildExpressionCore()
 61    {
 062        var parameter = Property.Parameters[0];
 063        var propertyBody = Property.Body;
 64
 65        // Build: Values.Any(v => EqualityComparer<TValue>.Default.Equals(v, x.Property))
 66        // Simplified approach: use a captured HashSet for O(1) lookups
 067        var values = new HashSet<TValue>(Values ?? []);
 068        var valuesConstant = Expression.Constant(values, typeof(HashSet<TValue>));
 069        var containsMethod = typeof(HashSet<TValue>).GetMethod(nameof(HashSet<>.Contains))!;
 070        var containsCall = Expression.Call(valuesConstant, containsMethod, Expression.Convert(propertyBody, typeof(TValu
 71
 072        Expression body = Operator switch
 073        {
 074            BinaryOperator.Is => containsCall,
 075            BinaryOperator.IsNot => Expression.Not(containsCall),
 076            _ => throw new NotSupportedException($"Operator {Operator} is not supported")
 077        };
 78
 079        return Expression.Lambda<Func<T, bool>>(body, parameter);
 80    }
 81
 82    /// <summary>
 83    /// Resets the filter to its default state by clearing the values.
 84    /// </summary>
 085    public override void Reset() => Values = null;
 86}
 87