简体   繁体   English

实体框架/ C#:字符串数组包含多个?

[英]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). 如果要对某些实体框架选择使用Include()方法(以避免“对象被处置”异常)。 But Include only accepts one string as parameter which means one include only. 但是Include仅接受一个字符串作为参数,这意味着仅包含一个。 To do multiple include, you must chain includes .Include("something").Include("something").Include("something") 要进行多个包含,您必须链接包括.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]) 所以我想写的是等价于.Include(array[0]).Include(array[1]).Include(array[2])...Include(array[n])

(Where 'n' = array.Length - 1) (其中'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: 您可以使用params string[]来使代码更清晰:

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. 对于调查此问题的其他人,我通过结合使用@ haim770和@daryal解决方案提出了一个解决方案。 It extends a given Context DbSet asQueryble and updates a local variable and keeps appending, based on the string[] includes parameter 它基于string[] includes参数扩展给定的Context DbSet asQueryble并更新局部变量并保持追加

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. 我希望这可以帮助人们寻找答案,即使问题有点老了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 实体框架分页子项包括 - C# Entity Framework Pagination Children Includes 在来自实体框架的查询中检查字符串是否不包含 C# 中的字符串数组的字符串 - Check if string does not contain strings of an array of string in C# in query from Entity Framework 一个连接字符串中的多个 IP 地址 C# 实体框架 - Multiple IP address in one connection string C# Entity Framework C#实体框架Json反序列化字符串数组问题 - C# Entity Framework Json deserialize String array issues 如果C#Windows Service包含(嵌入式)实体框架,则不会安装 - C# Windows Service wont install if it includes (embedded) Entity Framework C# 实体框架 fromsqlraw 查询,包含(in)而不是 where 子句 - C# entity framework fromsqlraw query with includes (in) instead of where clause 来自实体框架的FirstOrDefault中的C#String.Compare - C# String.Compare in FirstOrDefault from entity framework 比较mysql数据库和实体框架c#的字符串日期 - Compare String dates from mysql database with entity framework c# 实体框架 C# 从字符串参数查询 - Entity Framework C# queries from string parameters C#实体框架字符串规范函数 - C# entity framework String Canonical Functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM