1

I am trying to pass a List<string> to a method that will take it and use it to create a list of include statements to be used in an entity framework query.

For example:

List<string> myIncludes = new List<string>();
myIncludes.Add("myObject.FirstRelatedObject");
myIncludes.Add("myObject.SecondRelatedObject");

I want to use this list to get something like the following, dyamically:

var myQry = objectContext.object.Include(myIncludes[0]).Include(myIncludes[1]);

How would I go about doing this? I am using predicateBuilder to generate the "where" part of the statement, but I don't think that's the same thing as the "Include" portion.

1 Answer 1

5

You can try something like this (where predicate is your PredicateBuilder):

var includes = new List<string>() { "Include1, Include2" };

var query = this.Context.Campos;

foreach(var s in includes)
    query.Include(s);

var result = query.Where(predicate.Expand());
Sign up to request clarification or add additional context in comments.

1 Comment

So very right... I guess I've been so wrapped up in creating predicates, that I didn't see the simple solution right in front of me. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.