I recently inherited an ASP.NET Entity Framework API project.
I managed to do basic changes that were needed, but I'm getting a little lost with advanced SQL requests (I'm more a Symfony dev, I don't know Entity Framework).
I need to add a GET method to my API that will take a service (prestation) and will return a list of all the partners (Partenaires) who has a service (Prestation) with the same name as the GET parameter.
Here's the actual prototype of the method:
// 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
}
I have 3 tables that I have to use:
Prestation, Partenaires, PartenairesPrestation
Here are the 3 tables classes:
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")]
}
The last one makes the link between Partenaires
and Prestation
via IdPartenaire
and IdPrestation
columns.
Here's a screenshot of my model structure:
https://imageshack.com/a/img922/9111/4twiC8.png (imgur is not responding sorry)
Here's the PartenaireMapItem model:
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; }
}
}
I need to use join to get a list of partners (Partenaires
) that have the service (Prestation
) with the service name provided in the GET
parameter, but I don't know how to do it.
Any help would be welcome, thanks to anyone who will take the time to read/answer me.