【问题标题】:Add Include Expressions dynamically [duplicate]动态添加包含表达式[重复]
【发布时间】:2016-06-28 17:59:53
【问题描述】:

我正在使用 Entity Framework Core,我有以下内容:

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

这表示在查询中包括 Country、User 和 User.Country:

var q = context.Jobs
          .Include(x => x.Country)
          .Include(x => x.User).ThenInclude(x => x.Country);

我不知道表达式会包含什么。我只知道这将是一个实体列表,包含或不包含子实体(例如:User.Country),我需要构建 Include 表达式。

有没有办法做到这一点?

【问题讨论】:

  • 查看重复,只需拆分字符串,就可以得到一个数组。

标签: c# entity-framework linq entity-framework-core


【解决方案1】:

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

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

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

var q = context.Jobs;

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

【讨论】:

  • 如果我需要包含集合属性及其相应的属性怎么办?这里我们需要 .ThenInclude() 但这个答案不包括这种情况。
猜你喜欢
  • 2014-09-19
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多