从SQL Server数据库中选择linq两个对象

时间:2011-12-06 13:05:42

标签: c# sql-server-2008 linq-to-sql select

我有以下Log

int LogID
text Name
datetime CreationTime
text Content
int CreatorID

我尝试获取具体Creator

创建的所有日志
Creator creator = myDataContext.Creator.Single<Creator>(cr => cr.Name == name);
var query = (from l in myDataContext.Log
            where l.CreatorID == creator.CreatorID
            select l).ToList<Log>();

但在返回的列表中Creator为空。

如何获取给定Creator的日志列表?

2 个答案:

答案 0 :(得分:3)

您需要设置LoadOptions:

DataLoadOptions dataLoadOptions = new DataLoadOptions();
dataLoadOptions.LoadWith<Log>(l => l.Creator);
myDataContext.LoadOptions = dataLoadOptions;

答案 1 :(得分:0)

var logs = myDataContext.Logs.Include("Creator").Where(c => c.Creator.Name == name).ToList();