Saint
Saint

Reputation: 5469

Select with linq two object from SQL Server database

I've got the following Log table

int LogID
text Name
datetime CreationTime
text Content
int CreatorID

I try to get all the logs created by a concrete 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>();

but in the returned list the Creator is null.

How can I get a list of logs for a given Creator?

Upvotes: 1

Views: 236

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273179

You need to set LoadOptions:

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

Upvotes: 3

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13574

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

Upvotes: 0

Related Questions