0

I'm looking to dynamically select fields from a linq query where the fields names are in a csv string.

public IEnumerable<object> GetAllPeople(string fields)
{
   return (from p in people
           where p.alive == true
           select (the csv fields parameter (ie. fields = Name,Age,Height);
}

Yes, I'm using this as sort of a poor mans OData because I can't use OData where I am.

6
  • 1
    You are going to need to use expression trees. Commented Nov 17, 2016 at 16:55
  • codeblog.jonskeet.uk/category/linq Commented Nov 17, 2016 at 16:58
  • Can you elaborate the question more; what type is people and what exactly do you want to extract from fields (a specific field, all the fields, ect.)? Off the cuff I would think you can accomplish what you want by fields.split(",") Commented Nov 17, 2016 at 17:04
  • @Danny People is just a list of Person objects. I guess I would return object instead of a Person class from this function since ideally fields parameter is a csv list of fields in the Person object that I only want to return instead of returning ALL fields of a Person. Commented Nov 17, 2016 at 17:07
  • 2
    Your method should return a list of Person but your Select-statement just returns a subset of the properties of a Person? This won´t work at all, either return a Person or just parts of it - which would be an anoynmous type which you can´t return effectivly from a method. You might return IEnumerable<object>, but what are you doing with the instances within that list? You can´t cast them to their actual type as it was anonymous. Commented Nov 17, 2016 at 17:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.