c# every property of object linq code example
Example: 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);
}
}
}