I have two sql database tables with a 1:n relationship. For my ASP.NET MVC-solution I have enabled EF-code-first-migration and the proper DBContext and classes established.
I would like to write an MVC-controller that joins both tables in order to select specific records for display in a view.
Here are the two classes:
public class Tbl_Group_Communities : Entity
{
public string GKZ { get; set; }
public int G_ID { get; set; }
}
public class Katastralgemeinden : Entity
{
public string KGNr { get; set; }
public string KGName { get; set; }
public string GKZ { get; set; }
public string GemeindeName { get; set; }
}
So far I have been able to come up with a working controller for the tables by themselves but not joined. Below the working controller for the first class:
public IEnumerable<Tbl_Group_Communities> Get()
{
var entities = UnitOfWork.GetAll<Tbl_Group_Communities>().ToList();
return entities;
}
I think, the join can be done with Linq but I have no idea how/where to start. The common key for both tables is GKZ; so the join should be established via GKZ. And then I need to select specific records from the joined records where G_ID = a certain value.
If someone could give me some help, I'd be very thankful. Manu
Joinstatement? Apart from that, it's not clear whatUnitOfWorkcan produce. Does it also provideKatastralgemeinden? And if so, what doesGetAllreturn? If it'sIEnumerable, joining isn't efficient at all.IEnumerables, the data of both tables will be fetched into memory by two queries (that probably return lots of data) and then joined in memory. When you join twoIQueryable(coming from the same context), the join will be translated into one efficient SQL query.