简体   繁体   中英

Filter Type.GetProperties() where PropertyType.Name is in a List

I need to show only the properties that have names that are in the requiredfield list.

I'm trying to do something like this but p.PropertyType.Name == x is not correct:

Pricing pricing = new Pricing();
Type type = typeof(Pricing);
PropertyInfo[] PricingProperties = type.GetProperties();

PricingRequiredFieldDAL requiredField = new PricingRequiredFieldDAL();

var x = requiredField.GetRequiredFields();

var list = PricingProperties.Where(p => p.PropertyType.Name == x);

public class PricingRequiredFieldDAL
{
    PricingContext db = new PricingContext();

    public List<PricingRequiredField> GetRequiredFields()
    {
        return db.PricingRequiredFields.Where(p => p.Required == true).ToList();
    }
}

How would I go about getting the information I want using reflection in the above fashion?

Just something like:

var fieldNames = new HashSet<string>(x.Select(p => p.Name));
var properties = PricingProperties.Where(p => fieldNames.Contains(p.Name));

That's assuming that PricingRequiredField has a property called Name . You haven't actually told us that.

Try this:

var list = PricingProperties.Where(p => x.Contains(p.PropertyType.Name));

I supposed x is a list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM