Created
January 14, 2025 09:04
-
-
Save PiotrFerenc/ede91138ca008d8e4bc9a78eeb3653e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tworzenie wyrażenia "x => x.Property != null && x.Property.Contains(value)" | |
var parameter = propertyExpression.Parameters[0]; // Pobierz parametr lambda z propertyExpression (np. x) | |
var property = propertyExpression.Body; // Pobierz ciało wyrażenia (np. x.Property) | |
// Utwórz wyrażenie "x.Property != null" | |
var notNullExpression = Expression.NotEqual(property, Expression.Constant(null, typeof(string))); | |
// Wyszukaj metodę "Contains" na typie string | |
var containsMethod = typeof(string).GetMethod(nameof(string.Contains), new[] { typeof(string) }); | |
if (containsMethod == null) | |
throw new InvalidOperationException("Could not find 'Contains' method on the string class."); | |
// Utwórz wyrażenie "x.Property.Contains(value)" | |
var containsExpression = Expression.Call(property, containsMethod, Expression.Constant(value)); | |
// Połącz warunki "x.Property != null" AND "x.Property.Contains(value)" | |
var combinedExpression = Expression.AndAlso(notNullExpression, containsExpression); | |
// Stwórz pełne wyrażenie lambda: "x => x.Property != null && x.Property.Contains(value)" | |
var lambda = Expression.Lambda<Func<T, bool>>(combinedExpression, parameter); | |
// Dodaj wyrażenie do zapytania | |
_items = _items.Where(lambda); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment