Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态添加包含表达式_C#_Entity Framework_Linq_Entity Framework Core - Fatal编程技术网

C# 动态添加包含表达式

C# 动态添加包含表达式,c#,entity-framework,linq,entity-framework-core,C#,Entity Framework,Linq,Entity Framework Core,我使用的是实体框架核心,我有以下几点: String expression = "Country;User;User.Country" 这表示在查询中包括国家、用户和用户。国家: var q = context.Jobs .Include(x => x.Country) .Include(x => x.User).ThenInclude(x => x.Country); 我不知道将包含什么表达式。我只知道它将是一个实体列表,包含或不包

我使用的是实体框架核心,我有以下几点:

String expression = "Country;User;User.Country"
这表示在查询中包括国家、用户和用户。国家:

var q = context.Jobs
          .Include(x => x.Country)
          .Include(x => x.User).ThenInclude(x => x.Country);
我不知道将包含什么表达式。我只知道它将是一个实体列表,包含或不包含子实体(例如:User.Country),我需要构建Include表达式


有办法做到这一点吗?

有两种方法可以调用include方法。一个是表达式,另一个是字符串

String expression = "Country;User;User.Country"

string includes = expression.split(';');

var q = context.Jobs;

foreach (string include in includes)
    q = q.Include(include);

有两种方法可以调用include方法。一个是表达式,另一个是字符串

String expression = "Country;User;User.Country"

string includes = expression.split(';');

var q = context.Jobs;

foreach (string include in includes)
    q = q.Include(include);

请参阅复制,仅拆分字符串以获得数组。请参阅复制,仅拆分字符串以获得数组。