实体框架/ C#:从多个字符串数组包含? [英] Entity Framework/C#: multiple includes from string array?

查看:122
本文介绍了实体框架/ C#:从多个字符串数组包含?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果想使用包括()方法上的一些实体框架的选择(为了避免'对象已处置除外)。
,但包括只接受一个字符串作为参数,它是指一个只包含。
。要做到多个包含,则必须链包括 .INCLUDE(东西)。包括(东西)。包括(东西)

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.

所以,我希望写的是相当于.INCLUDE(数组[0])。包括(数组[1])。包括(数组[2])...包括(数组[n])

(其中n = array.Length - 1)

(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

编辑:鉴于到目前为止,我已经有suggetions,我会说请得到准确的类型,避免空值的问题并对其进行测试。到目前为止,没有任何解决方案似乎工作,我得到什么,我可以和不可以用这样或那样的类型做丢失。

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.

推荐答案

您可以使用 PARAMS字符串[] 这将会使你的代码更加清晰:

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");

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

这篇关于实体框架/ C#:从多个字符串数组包含?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆