mardi 27 octobre 2020

get all string properties from an object that have multiple object inside

I have an object that have multiple object inside and properties , i want to get all the values of string type of all the object inside it until the last depth , i'm not too familiar with linq and reflection principal , that what i achieved so far

  public void Recherche()
    {
        _viewListeVente.Refresh();
       
        _viewListeVente.Filter = new Predicate<object>(Contain);
    }
    public bool Contain(object de)
    {
        SalesCounter item = de as SalesCounter;
        
        return de.GetType().GetProperties()
.Where(pi => pi.PropertyType==typeof(string))
.Select(pi => (string)pi.GetValue(de))
.Any(value => value.Contains(rechercheProduitText));




    }

and that's an example of a model i'm using

 public class SalesCounter
{
    public Client client { get; set; }
    public string price { get; set; }
    public string reference { get; set; }
    public string date { get; set; }
    public int discount { get; set; }
    public string discountType { get; set; }
    public Item[] items { get; set; }
    public int amount { get; set; }

    public string _id { get; set; }
}

I know that i can return all object of type class, but those objects also have objects inside so i'm searching a generic way to get all the way inside and get all strings





Aucun commentaire:

Enregistrer un commentaire