Linq result from many tables into an inherit class with nested classes

c# entity-framework-6 linq nested

Question

I have a SQL table, but the client erased every foreign key and won't allow me to recreate the table with them all.

I have something like this since I utilised Entity Framework with a database-first approach:

public class MainTable
{
    MainTableID,
    TableChildOneID,
    TableChildTwoID,
    //... many properties
}

public class TableChildOne
{
    TableChildOneID,
    //... many properties
}

public class TableChildTwo
{
    TableChildTwoID,
    //... many properties
}

I made a class to represent the "actual" linked nested class, which looks like this:

public class MainTable_Extended : MainTable
{
    public TableChildOne ChildOne { get; set; }
    public TableChildTwo ChildTwo { get; set; }
}

Keep in mind that the MainTable has several characteristics.

I'm searching for a tidy syntax in this query.

IQueryable<MainTable_Extended> listMainTable =
            from main in db.MainTable
            from childone in db.TableChildOne.Where(childone => childone.TableChildOneID == main.TableChildOneID).DefaultIfEmpty()
            from childtwo in db.TableChildTwo.Where(childtwo => childtwo.TableChildTwoID == main.TableChildTwoID).DefaultIfEmpty()
            select new MainTable_Extended
            {
                ChildOne = childone ,
                ChildTwo = childtwo ,
                All others properties coming from => main 
            };

How can I fill in every property of my class MainTable Extended without having to do it by hand?

1
3
5/29/2018 9:32:47 AM

Accepted Answer

ZZZ_tmp
1
5/29/2018 12:59:07 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow