Linq 从许多表生成一个带有嵌套类的继承类

我有一个 SQL 表,但客户删除了所有外键,并且不会让我用所有外键重建表。


我在数据库优先方法上使用了实体框架,我有类似的东西:


public class MainTable

{

    MainTableID,

    TableChildOneID,

    TableChildTwoID,

    //... many properties

}


public class TableChildOne

{

    TableChildOneID,

    //... many properties

}


public class TableChildTwo

{

    TableChildTwoID,

    //... many properties

}

我创建了一个代表“真实”链接嵌套类的类,例如:


public class MainTable_Extended : MainTable

{

    public TableChildOne ChildOne { get; set; }

    public TableChildTwo ChildTwo { get; set; }

}

注意 MainTable 包含很多属性


这是我正在寻找干净语法的查询


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 

            };

如何填充我的类 MainTable_Extended 而无需为所有属性手动填充。


长风秋雁
浏览 165回答 1
1回答

大话西游666

也许这对你有用?public class MainTable&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;public int MainTableID;&nbsp; &nbsp; &nbsp; &nbsp;public int TableChildOneID;&nbsp; &nbsp; &nbsp; &nbsp;public int TableChildTwoID;&nbsp; &nbsp; &nbsp; &nbsp;//... many properties&nbsp; &nbsp;}public class TableChildOne&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;public int TableChildOneID;&nbsp; &nbsp; &nbsp; &nbsp;//... many properties&nbsp; &nbsp;}public class TableChildTwo&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;public int TableChildTwoID;&nbsp; &nbsp; &nbsp; &nbsp;//... many properties&nbsp; &nbsp;}public class MainTable_Extended&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;public TableChildOne ChildOne { get; set; }&nbsp; &nbsp; &nbsp; &nbsp;public TableChildTwo ChildTwo { get; set; }&nbsp; &nbsp; &nbsp; &nbsp;public MainTable Main { get; set; }&nbsp; &nbsp;}public class DB : DbContext&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;public DB()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: base("name=YourName")&nbsp; &nbsp; &nbsp; &nbsp;{}&nbsp; &nbsp; &nbsp; &nbsp;public virtual DbSet<MainTable> MainTables { get; set; }&nbsp; &nbsp; &nbsp; &nbsp;public virtual DbSet<TableChildOne> TableChildOnes { get; set; }&nbsp; &nbsp; &nbsp; &nbsp;public virtual DbSet<TableChildTwo> TableChildTwos { get; set; }&nbsp; &nbsp;}public class Test&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; public void TestTest()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var db = new DB();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IQueryable<MainTable_Extended> listMainTable =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;from main in db.MainTables&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;from childone in db.TableChildOnes.Where(childone => childone.TableChildOneID == main.TableChildOneID).DefaultIfEmpty()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;from childtwo in db.TableChildTwos.Where(childtwo => childtwo.TableChildTwoID == main.TableChildTwoID).DefaultIfEmpty()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select new MainTable_Extended&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ChildOne = childone,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ChildTwo = childtwo,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Main = main&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }我让MainTable_Extended classnot inhier 从MainTable class. 相反,我使用 MainTable 对象创建了一个属性。所以在查询中我给出MainTable_Extended了属性的值。因此,没有可以查询的propertysMainTable通过调用属性Main的MainTable_Extended。
打开App,查看更多内容
随时随地看视频慕课网APP