I am working on a project in C# using Entity Framework. Using EF, I connect to database that has a dozen of tables, and none of the views. What I want to accomplish is create object in code that would represent sql view (i.e. three DB tables joined for presenting useful data). I need this so I could create binding data source for the datagirdview. Right now I manually wrote SQL query for joining three tables and creating a list which then I define as datagridview.DataSource
This troubles me because of hard design about presenting the data in the datagridview. I have to manually write everything in code, and also not sure how to accomplish everything I need.
I would be more satisfied if I could create new class that would represent above and then in Designer View add Data source as object and view it in Designer mode and edit columns right there.
My query which does the job is as follows:
using (var context = new csModelEntitites())
{
var listaVozila = (from voz in context.Vozilo
join var in context.Varijanta on voz.VarijantaID equals var.ID
join mod in context.Model on var.ModelID equals mod.ID
join mar in context.Marka on mod.MarkaID equals mar.ID
select new
{
voz.ID,
voz.VIN,
Vozilo = mar.Naziv + " " + mod.Naziv + " " + var.Motor,
voz.GodProizvodnje,
voz.RegOznaka,
voz.RegDo
}).ToList();
VozilaPrikaz.DataSource = null;
VozilaPrikaz.DataSource = listaVozila;
foreach (DataGridViewColumn c in VozilaPrikaz.Columns)
{
if (c.HeaderText == "GodProizvodnje")
c.HeaderText = "Godina proizvodnje";
if (c.HeaderText == "RegOznaka")
c.HeaderText = "Registarska oznaka";
if (c.HeaderText == "RegDo")
c.HeaderText = "Registriran do";
I want to create class that would do the similar. Any help appreciated.
select new SomeClassWithProperties { ... }.