1 回答
TA贡献1817条经验 获得超14个赞
也许这对你有用?
public class MainTable
{
public int MainTableID;
public int TableChildOneID;
public int TableChildTwoID;
//... many properties
}
public class TableChildOne
{
public int TableChildOneID;
//... many properties
}
public class TableChildTwo
{
public int TableChildTwoID;
//... many properties
}
public class MainTable_Extended
{
public TableChildOne ChildOne { get; set; }
public TableChildTwo ChildTwo { get; set; }
public MainTable Main { get; set; }
}
public class DB : DbContext
{
public DB()
: base("name=YourName")
{}
public virtual DbSet<MainTable> MainTables { get; set; }
public virtual DbSet<TableChildOne> TableChildOnes { get; set; }
public virtual DbSet<TableChildTwo> TableChildTwos { get; set; }
}
public class Test
{
public void TestTest()
{
var db = new DB();
IQueryable<MainTable_Extended> listMainTable =
from main in db.MainTables
from childone in db.TableChildOnes.Where(childone => childone.TableChildOneID == main.TableChildOneID).DefaultIfEmpty()
from childtwo in db.TableChildTwos.Where(childtwo => childtwo.TableChildTwoID == main.TableChildTwoID).DefaultIfEmpty()
select new MainTable_Extended
{
ChildOne = childone,
ChildTwo = childtwo,
Main = main
};
}
}
我让MainTable_Extended classnot inhier 从MainTable class. 相反,我使用 MainTable 对象创建了一个属性。所以在查询中我给出MainTable_Extended了属性的值。
因此,没有可以查询的propertysMainTable通过调用属性Main的MainTable_Extended。
- 1 回答
- 0 关注
- 162 浏览
添加回答
举报