简体   繁体   English

如何编写这个 LINQ 表达式?

[英]How can I compose this LINQ expression?

I am creating a web application to manage a flight school and I have the following database structure (unrelevant tables have been blurred and relevant ones have been translated to English for clarity):我正在创建一个 web 应用程序来管理飞行学校,并且我具有以下数据库结构(不相关的表格已被模糊,相关表格已被翻译成英文以清楚起见):

在此处输入图像描述

I have a table Deadlines that stores all deadlines related to an aircraft.我有一个表格截止日期,其中存储与飞机相关的所有截止日期。 This table is linked to the Aircrafts table.此表链接到Aircrafts表。 Then I have a OwnedAircrafts table that stores ids of aircrafts and persons (because a person can own several aircrafts or shares of them).然后我有一个OwnedAircrafts表,其中存储了飞机和人员的 ID(因为一个人可以拥有几架飞机或其中的一部分)。

My question is:我的问题是:

Given the id of a person, how can I retrieve the list of deadlines related to his/her aircrafts?给定一个人的 id,我如何检索与他/她的飞机相关的截止日期列表? (preferrably using method syntax) (最好使用方法语法)

In other words:换句话说:

The following code now returns all the aircraft deadlines, no matter who the owner is.下面的代码现在返回所有飞机的最后期限,无论所有者是谁。 How can I reference the OwnedAircrafts table to filter only the deadlines related to personId ?如何参考OwnedAircrafts表以仅过滤与personId相关的截止日期?

    IEnumerable<DeadlineViewModel> GetDeadlinesOfAPerson(long personId)
    {
        return _context.Deadlines
            .Select(deadline => new DeadlineViewModel
            {
                Id = deadline.Id,
                Aircraft = deadline.Aircraft,
                Model = deadline.AircraftsNavigation.Model,
                IdDeadlineType = deadline.DeadlineType,
                DeadlineType = deadline.DeadlineTypesNavigation.Description
                // other fields...
            });

Thank you in advance!先感谢您!

Try following:尝试以下操作:

    IEnumerable<DeadlineViewModel> GetDeadlinesOfAPerson(long personId)
    {
        return _context.Deadlines
            .Where(x => x.deadline.ID == 12345)
            .Select(deadline => new DeadlineViewModel
            {
                Id = deadline.Id,
                Aircraft = deadline.Aircraft,
                Model = deadline.AircraftsNavigation.Model,
                IdDeadlineType = deadline.DeadlineType,
                DeadlineType = deadline.DeadlineTypesNavigation.Description
                // other fields...
            });

Follow this approach:遵循这种方法:

 IEnumerable<DeadlineViewModel> GetDeadlinesOfAPerson(long personId)
{
    return _context
        .OwnedAircarfs
        .Where( s => s.Persona == YourPersonId)
        .Includes(s=> s.AirCrafts)
          .ThenIncludes(s=> s.Deadlines)
       
        .Select(deadline => new DeadlineViewModel
        {
             //use navigation properties to get value of related tables
            // other fields...
        }).ToList();

You can add navigation properties to your entities and then try something like this:您可以将导航属性添加到您的实体,然后尝试这样的事情:

var user = _context.Users
    .Include(user => user.OwnedAircrafts)
        .ThenInclude(ownedAircraft => ownedAircraft.Aircraft)
            .ThenInclude(aircraft => aircraft.Deadlines)
    .FirstOrDefault(user => user.Id == userId)

return user.OwnedAircrafts
    .SelectMany(ownedAircraft => ownedAircraft.Aircraft.Deadlines);

You do not need to query the Persons table, as the person Id is available in OwendAircrafts您不需要查询 Persons 表,因为OwendAircrafts中提供了人员 ID

IEnumerable<DeadlineViewModel> GetDeadlinesOfAPerson(long personId)
{
    return _context.Deadlines
        .Where(deadline =>
            deadline.Aircraft.OwnedAircrafts.Any(oa => oa.persona == personId))
        .Select(deadline => new DeadlineViewModel
        {
            Id = deadline.Id,
            Aircraft = deadline.Aircraft,
            Model = deadline.AircraftsNavigation.Model,
            IdDeadlineType = deadline.DeadlineType,
            DeadlineType = deadline.DeadlineTypesNavigation.Description
            // other fields...
        });
}

This assumes that you have a navigation property OwnedAircrafts in Aircraft .这假设您在Aircraft中有一个导航属性OwnedAircrafts

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM