简体   繁体   中英

Entity Framework/C#: multiple includes from string array?

If want to use the Include() method on some Entity Framework selection (in order to avoid the 'Object as been disposed' exception). But Include only accepts one string as parameter which means one include only. To do multiple include, you must chain includes .Include("something").Include("something").Include("something")

But I would like my includes to come from a string array.

So what I would want to write is the equivalent of .Include(array[0]).Include(array[1]).Include(array[2])...Include(array[n])

(Where 'n' = array.Length - 1)

Of course I don't know in advance what will be in the string array.

But I can't find the correct syntax so far. Thank you for your help

EDIT: Given the suggetions I've had so far, I'd say please be precise about type and avoiding null value problems and test it. So far no solution seems to work and I get lost in what I can and can't do with this or that type.

You can use params string[] which will make your code even clearer:

public static IQueryable<T> Include<T>(this IDbSet<T> dbSet, params string[] includes) where T : class
{
    foreach (var include in includes)
        dbSet.Include(include);

    return dbSet;
}

Then you can use it both ways:

.Include("NavProp1", "NavProp2", "NavProp3");

And:

.Include(new[] { "NavProp1", "NavProp2", "NavProp3" });

You can always chain them in a loop:

var query = DataSource.Select(...).Where(...); // as IQueryable<T>
foreach(string include in includes)
{
   query = query.Include(include);
}

return query.ToList(); // materialize
    public IEnumerable<T> GetDataWithIncludes<T>(string[] includes) where T: class, new()
    {
        DbQuery<T> dbQuery = null;
        DbSet<T> dbSet = this.Set<T>();
        foreach (string include in includes)
        {
            dbQuery = dbSet.Include(include);
        }

        return dbQuery.AsEnumerable();
    }

To other people investigating this issue, I've came up with a solution by combining @haim770 and @daryal solutions. It extends a given Context DbSet asQueryble and updates a local variable and keeps appending, based on the string[] includes parameter

Extension Class

public static class Extensions
    {
        public static IQueryable<T> IncludeMany<T>(this DbSet<T> model, params string[] includes)
            where T : class
        {
            var modelQuerable = model.AsQueryable();
            foreach (var value in includes)
            {
                modelQuerable = modelQuerable.Include(value);
            }
            return modelQuerable;
        }
}

Implementation

public async Task<TestModel> GetData(Guid id)
        {
            var testModel= await context.TestModel.IncludeMany(
                    "xx",
                    "yy",
                    "xx.zz"
                    ).All();
            return testModel;
        }

I hope this helps people looking for answers, even though the question is a bit old.

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