mardi 23 juillet 2019

Find a string in a list of strings in c# with reflection

List<string> typesList = new List<string>();
typesList.Add("product");
typesList.Add("valueType");
typesList.Add("categoryType");

foreach (var prop in someType.GetProperties())
{
    string filterValue = Filter.SecondStringByProp(prop, filterList, "_");//this return `valueType`

    bool wtf1 = typesList.Any(x => x == prop.Name.ToLower());
    bool wtf2 = typesList.Contains(prop.Name.ToLower());
    var wtf3 = typesList.Where(x => x == prop.Name.ToLower()).FirstOrDefault();
    bool wtf4 = string.IsNullOrEmpty(typesList.Where(x => x == prop.Name.ToLower()).FirstOrDefault());
    bool wtf5 = !string.IsNullOrEmpty(typesList.Where(x => x == prop.Name.ToLower()).FirstOrDefault());
    bool wtf6 = typesList.Any(s => s.Contains(prop.Name.ToLower()));
}

answer that always work but not now!

I do not understand why so many different ways that I think should work DO NOT WORK - to return bool

the above code is checking if prop.Name.ToLower() which is valueType is equal to element from collection typesList (valueType)

wtf1 and wtf2 - just for testing purpose and curiosity wtf4 and wtf5 - this just return the expected value wtf6 - wtf why return false?!





Aucun commentaire:

Enregistrer un commentaire