是否可以在单个查询中选择映射的实体中的另一个映射实体?

发布于 2025-02-06 16:01:34 字数 1330 浏览 1 评论 0原文

是否有任何优雅的方法可以将嵌套的LINQ选择用于自己选择的对象?换句话说,让我们假设有三个DB表与一对多关系:时间表,日(一个时间表可能有很多天)和活动(一天可能有很多活动。我想尝试构建查询哪些数据无需创建其他辅助对象,这是可能

public Class Schedule{
   public int ScheduleId { get; set; }
   ...
   public byte[] RowVersion { get; set; }

   [NotMapped]
   public List<Day> days; //Supposed to store Day objects
}

public Class Day{
   public int DayId { get; set; }
   ...
   public byte[] RowVersion { get; set; }

   [NotMapped]
   public List<Activity> activities; //Supposed to store Activity objects
}

public Class Activity{
   public int ActivityId { get; set; }
   ...
   public byte[] RowVersion { get; set; }
}

的被执行,我将PRIMS放入适当的对象的[未图表]属性(请参阅上文),然后删除Prims,这似乎是使用不必要的资源

from schedules in context.Schedules.Where(...)
select new SchedulePrim
{
    Schedule = schedules
    DaysPrim = from days in context.Days.Where(...)
               select new DaysPrim
               {
                    Day = days
                    ActivitiesPrim = from activities in context.Activities.Where(...)
                                     select new DaysPrim
                                     {
                                          Activity = activities
                                     }
               }
}

。实体。 有更快的方法吗?可以将数据选择到[未映射]属性的方式,而无需引入其他处理?

Is there any elegant way to perform a nested LINQ selection into objects that are being selected themselves? In other words, let's assume there are three DB tables all with one-to-many relation: Schedule, Day (One schedule may have many days) and Activity (One day may have many activities. I would like to try to build a query which selects data into related mapped objects without the necessity of creating additional helper objects. Is that even possible? Please see below objects which map DB tables:

public Class Schedule{
   public int ScheduleId { get; set; }
   ...
   public byte[] RowVersion { get; set; }

   [NotMapped]
   public List<Day> days; //Supposed to store Day objects
}

public Class Day{
   public int DayId { get; set; }
   ...
   public byte[] RowVersion { get; set; }

   [NotMapped]
   public List<Activity> activities; //Supposed to store Activity objects
}

public Class Activity{
   public int ActivityId { get; set; }
   ...
   public byte[] RowVersion { get; set; }
}

At this point I use additional classes to fetch data: SchedulePrim, DaysPrim and ActivitiesPrim. After the query is executed I put Prims into [NotMapped] attributes of proper objects (see above) and then get rid of Prims. To me this seem like using unnecessary resources. The query looks somewhat like this:

from schedules in context.Schedules.Where(...)
select new SchedulePrim
{
    Schedule = schedules
    DaysPrim = from days in context.Days.Where(...)
               select new DaysPrim
               {
                    Day = days
                    ActivitiesPrim = from activities in context.Activities.Where(...)
                                     select new DaysPrim
                                     {
                                          Activity = activities
                                     }
               }
}

Here comes the logic of reprocessing fetched data into proper entities.
Is there a faster way to do this? The way that lets selecting data into [NotMapped] attributes on the fly, without the need of introducing additional processing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2025-02-13 16:01:34

只需消除未映射的属性和“其他类”,请确保您拥有适当的外键,并使用Include include加载数据。

db.Schedules.Include(s => s.Days).ThenInclude(d => d.Activities).Where(...)

Just eliminate the NotMapped attributes and the "additional classes", make sure you have proper foreign keys, and load the data using Include.

db.Schedules.Include(s => s.Days).ThenInclude(d => d.Activities).Where(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文