首页 > 解决方案 > 返回多个连接/导航对象

问题描述

我最近继承了一个 ASP.NET Entity Framework API 项目。

我设法进行了必要的基本更改,但我对高级 SQL 请求有点迷失(我更像是 Symfony 开发人员,我不知道实体框架)。

我需要在我的 API 中添加一个 GET 方法,该方法将采用服务(prestation)并将返回所有合作伙伴(Partenaires)的列表,这些合作伙伴(Partenaires)拥有与 GET 参数同名的服务(Prestation)。

这是该方法的实际原型:

// GET: api/Partenaires_prestations
[Authorize]
public List<PartenaireMapItem> GetPartenairesWithPrestations(string prestation = "")
{    
    if (string.IsNullOrWhiteSpace(prestation))
    {
        prestation = "";
    }

    return db.Partenaires
                .Join() // Here I don't know what to do
}

我有 3 个必须使用的表:

Prestation, Partenaires, PartenairesPrestation 

以下是 3 个表类:

namespace Uphair.EfModel
{
    using System;
    using System.Collections.Generic;

    public partial class Partenaire
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Partenaire()
        {
            this.PartenairePrestations = new HashSet<PartenairePrestation>(); // I took out the useless parts
        }

        public int IdPartenaire { get; set; }
        [...] // I took out properties that are not needed

        public virtual ICollection<PartenairePrestation> PartenairePrestations { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
}

namespace Uphair.EfModel
{
    using System;
    using System.Collections.Generic;

    public partial class PartenairePrestation
    {
        public int IdPartenaire { get; set; }
        public int IdPrestation { get; set; }
        public double Prix { get; set; }
        public int Duree { get; set; }
        public Nullable<System.DateTime> DateAjout { get; set; }

        public virtual Partenaire Partenaire { get; set; }
        public virtual Prestation Prestation { get; set; }
    }
}

namespace Uphair.EfModel
{
    using System;
    using System.Collections.Generic;

    public partial class Prestation
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Prestation()
        {
            this.PartenairePrestations = new HashSet<PartenairePrestation>();
            this.PrixDureeOptions = new HashSet<PrixDureeOption>();
            this.LigneReservations = new HashSet<LigneReservation>();
        }

        public int IdPrestation { get; set; }
        public string NomPrestation { get; set; }
        public int Categorie { get; set; }
        public Nullable<int> CoifEsthe { get; set; }
        public Nullable<int> IdPrestationCategorie { get; set; }

        public virtual ICollection<PartenairePrestation> PartenairePrestations { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
}

最后一个在PartenairesPrestationviaIdPartenaireIdPrestation列之间建立链接。

这是我的模型结构的屏幕截图:

https://imageshack.com/a/img922/9111/4twiC8.png(imgur 没有回应抱歉)

这是 PartenaireMapItem 模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Uphair.EfModel;

namespace Uphair.Api.Models.Partenaire
{
    public class PartenaireMapItem
    {
        public int IdPartenaire { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string NomComplet { get; set; }
        public double? Lat { get; set; }
        public double? Lng { get; set; }
        public PartenaireType Type { get; set; }

        public int DureeMin { get; set; }

        public string ImageUrl { get; set; }

        public int SeDeplace { get; set; }

        public bool ADomicile { get; set; }

        public double NoteGlobale { get; set; }


        public List<String> Prestations { get; set; } 
    }
}

我需要使用 join 来获取具有参数中提供的服务名称Partenaires的服务 ( ) 的合作伙伴 ( )列表,但我不知道该怎么做。PrestationGET

欢迎任何帮助,感谢任何愿意花时间阅读/回答我的人。

标签: c#asp.net-mvcvisual-studioentity-framework

解决方案


推荐阅读