linq to sql navigation properties simple example

时间:2016-04-07 10:49:01

标签: c# linq

For this kind of schema:

mainTable(Id,Name) //Id is primary key

secondryTable(mainTableId,RegDate,Age) // mainTableId is foreign key

Can anyone give me a example of Linq to Sql using navigation properites.

With join I am selecting as:

from mainT in db.mainTable
join secT in db.secondryTable on mainT.Id equals secT.mainTableId
select new { mainT.Name, secT.RegDate, secTable.Age}

1 个答案:

答案 0 :(得分:0)

我自己找到了一个解决方案:)查询将是:

FROM mainT in db.mainTable
SELECT new
    {
      mainT.Name,
      regDate = mainT.secT.Where(//some condition),
      age = mainT.secT.Where(//some condition)
    }

只有在数据库中正确定义了主键/外键关系时,上述查询才有效。