Answers for "c# every property of object linq"

C#
0

c# every property of object linq

public IEnumerable<KeyValuePair<string, IEnumerable<string>>> Dependencies(TEntity entity) 
 {
     if (entity == null)
     {
         yield break;
     }

     var properties = entity.GetType()
         .GetProperties()
         .Where(p => typeof(IEnumerable).IsAssignableFrom(p.PropertyType) 
                 && !typeof(string).IsAssignableFrom(p.PropertyType)
                 &&  p.CanRead && p.GetGetMethod() != null 
                 &&  p.GetIndexParameters().Length == 0);

     foreach (var property in properties)
     {
         if (property.GetValue(entity) is IEnumerable values)
         {
             var children = (
                 from object value 
                 in values
                 where value != null
                 select value.ToString()
             ).ToList();

             yield return new KeyValuePair<string, IEnumerable<string>>
                 (property.Name, children);
         }
     }
 }
Posted by: Guest on April-16-2021

C# Answers by Framework

Browse Popular Code Answers by Language