Check if any object property contains string

user7255640

I have a list of UserInformation

List<UserInformation> ui = new List<UserInformation>();

The UserInformation object looks like this;

public class UserInformation
{
    public UserInformation()
    {
    }

    public UserInformation(UserInformation u)
    {
        this.Id = u.Id;
        this.parentId = u.parentId;
        this.Name = u.Name;
        this.Title = u.Title;
        this.Department = u.Department;
        this.Image = u.Image;
        this.Parent = u.Parent;
        this.Username = u.Username;
        this.Company = u.Company;
        this.Initials = u.Initials;
        this.Disabled = u.Disabled;
    }

    public int Id { get; set; }
    public int? parentId { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public string Department { get; set; }
    public string Image { get; set; }
    public string Parent { get; set; }
    public string Username { get; set; }
    public string Company { get; set; }
    public string Initials { get; set; }
    public bool Disabled { get; set; }
}

Is there some way to check if any of these properties, contains a specific Word? Lets say ".test"?

Update

I kinda want to avoid something like

!new[] { ".ext", ".test", ".admin" }.Any(c => ui.Title.ToLower().Contains(c))
!new[] { ".ext", ".test", ".admin" }.Any(c => ui.Department.ToLower().Contains(c))
!new[] { ".ext", ".test", ".admin" }.Any(c => ui.Company.ToLower().Contains(c))
!new[] { ".ext", ".test", ".admin" }.Any(c => ui.Username.ToLower().Contains(c))
Tim Schmelter

You could use this method using reflection to get all properties that contain your text:

public static IEnumerable<PropertyInfo> PropertiesThatContainText<T>(T obj, string text, StringComparison comparison = StringComparison.Ordinal)
{
    var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
       .Where(p => p.PropertyType == typeof(string) && p.CanRead);
    foreach (PropertyInfo prop in properties)
    {
        string propVal = (string)prop.GetValue(obj, null);
        if (String.Equals(text, propVal, comparison)) yield return prop;
    }
}

If you just want to know if there was at least one property:

bool anyPropertyContainsText = PropertiesThatContainText(yourUserInfo, ".test").Any();

But in general i would avoid using reflection wherever possible. Instead create a method in UserInformation that checks the relevant properties explicitly. Or just check it where you have to know it. A little verbose but readable and everyone will understand your code including yourself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

[Python]Check if any string in a list is contains any string in another list

Check if a string contains any element of an array in JavaScript

Check if string contains any letter (Javascript/jquery)

Check If string contains any of the words

Check if a string contains particular characters in any order

check if any property in an object is nil - Swift 3

Check if column contains type string (object)

Check if string contains any keywords that exists in array

Filter IEnumerable<object> based on whether string property contains any string value of another List<string>

Check if string contains any string in list with forbiddenfruit

Is there a way to check if any existing object contains a specific string paramater?

Check if string contains any numbers in MATLAB

Liquid: check if object contains certain string

Check if a string contains any integers in SQLite

Check if any property in a object is empty

Check if a string contains any element of array

Check if array contains an object with the value of a specific property

check whether string contains any of the given charsequence

Check if string contains any elements from list

Check if string contains a list of strings in any order

Check if a json object contains a specific string with php

Get property from object and check if it contains value

How to check if a list of words contains any string

Check if a string contains any file extension whatsoever

How to check if array of object contains a string

Safe way to check if a property of an object contains a substring

check if string contains word in any variation

How to check with whole raw object (without using any property) if that object contains in array of objects in javascript?

Check if part of a string contains any numbers

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive