Reputation: 35
I am developing an API which fetches data from a database, i am using an Azure Function with Entity Framework. So far i can easily fetch data from one table without any issues, the problem starts when i try to combine multiple tables and return the results. My desired goal would be to return some information from the Person table combined with some information from the Profile Table. I am getting an error stating that it cannot implicitly convert the type even thought I have created a new type to set it as the return type of the function.
This is my database diagram:
My Context class:
public virtual DbSet<Profile> Profile { get; set; }
public virtual DbSet<ProfileSocialMedia> ProfileSocialMedia { get; set; }
public virtual DbSet<SocialMediaPlatform> SocialMediaPlatform { get; set; }
public Task<List<Profile>> GetProfileList()
{
var profiles = Profile.ToListAsync();
return profiles;
}
My HttpTrigger:
[FunctionName(nameof(GetProfiles))]
public Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "get-profiles")]
HttpRequest req,
ILogger log) => _helper.HandleAsync(async () =>
{
var leaders = await _context.GetProfileList();
return new OkObjectResult(leaders);
});
I have tried the following code:
My Attempt:
public Task<List<Profile>> GetProfileList()
{
var profiles = Person.Join(Profile, person => person.Id, profile => profile.PersonId,
(person, profile) => new
{
person.Id,
person.FirstName,
person.LastName,
profile.PreviousOccupation
}).ToListAsync();
return profiles;
}
Upvotes: 0
Views: 47
Reputation: 35
Ok, so in order to achieve what i wanted to achieve i had to create a new instance of the type i wanted to return like so:
public Task<List<LeaderProfileModel>> GetProfileList()
{
var profiles = Person.Join(Profile, person => person.Id, profile => profile.PersonId,
(person, profile) => new {person, profile})
.Where(f => f.person.FirstName == "Name" && f.person.LastName == "Nastname")
.Select(lpm => new LeaderProfileModel()
{
Id = lpm.person.Id,
FirstName = lpm.person.FirstName,
LastName = lpm.person.LastName,
PreviousOccupation = lpm.profile.PreviousOccupation
}).ToListAsync();
return profiles;
}
Upvotes: 2