I have six tables as shown below. I am trying to get data based on Application_name, Environment_name, and Status.
Status table:
Id, Name
Application table:
Id, Name
Servers table:
Id, ServerName, Status
Environments table:
Id, Name
ResourceGroup table:
Id, Name, Application_Id, Environment_Id
ServersResourceGroup:
Id, Server_Id, Resource_Id
What I am trying to do is join all the require table and use where to filter data by Application_name, Environment_name, and Status
Here is my query I built, which returns all the data back by filtering Application_name but I am no way near to fulfill above requirement by adding additional filters by Environment_name and Status :(
So below is the query that returns all the data with Application_name
public IEnumerable<ResourceGroup> GetAllServersByApplication(string application_name, string environment_name, string status)
{
var query = _context.ResourceGroup
.Include(a => a.Application)
.Include(t => t.Type)
.Include(e => e.ServersGroup).ThenInclude(e => e.Environment)
.Include(s => s.ServersGroup).ThenInclude(s => s.Server)
.Include(s => s.ServersGroup).ThenInclude(s => s.Server).ThenInclude(s => s.Status)
.Where(a => a.Application.Name == application_name)
.ToList();
return query;
}
Here is the query that I am trying to write that will filter based on all three filters:
public IEnumerable<ResourceGroup> GetAllServersByApplication(string application_name, string environment_name, string status)
{
var query = _context.ResourceGroup
.Include(a => a.Application)
.Include(t => t.Type)
.Include(e => e.ServersGroup).ThenInclude(e => e.Environment)
.Include(s => s.ServersGroup).ThenInclude(s => s.Server)
.Include(s => s.ServersGroup).ThenInclude(s => s.Server).ThenInclude(s => s.Status)
.Where(a => a.Application.Name == application_name)
.Select(e => e.ServersGroup.Where(s => s.Environment.Name == environment_name && s.Server.Status.Name == status)
.ToList();
return query;
}
I get a red line under return query. Please see below image:

Is there any easier way to write lambda query then what I am trying to do?
Any help is really appreciated. :)
Thanks,
Ray
Include()on everything if you are not going toSelectit. What I mean is if you don't need that data in your application then don't include it. TheWherewill work fine without the includes.