Q:

Adding a linked object to another object in C# ASP.Net MVC

I have a model as follows:
public class Analyst
{
    public int AnalystId { get; set; }
    public string AnalystName { get; set; }
    public string CorpId { get; set; }
    public string TeamLeader { get; set; }
    public string TLCorpId { get; set; }
    public IList<Objective> Objectives { get; set; }
    public IList<ObScore> ObScores { get; set; }
}

public class Objective
{
    public int ObjectiveId { get; set; }
    public int AnalystId { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int Weight { get; set; }
    public Analyst Analyst { get; set; }
}

I am trying to find a specific Objective which I can do with:
Objective objective = db.Objectives.Find(id);

...however I would also like to include the Analyst associated with the objective, so I ca reference a property on the oinked Analyst object:
if (objective.Analyst.TLCorpId != tl)

But when I'm trying to add the analyst object I get an error:
Objective objective = db.Objectives.Include(x => x.Analyst).Find(id);

System.Linq.IQueryable<Objectives.Models.Objective> does not contain a definition for 'Find' and no extension method 'Find'
  accepting a first argument of type
  System.Linq.IQueryable<Objectives.Models.Objective> could be found
  (are you missing a using directive or an assembly reference?)

Is there any way around this please?

A:

Method Find defined on DbSet<T>. Method Include is defined on base class DbQuery<T>. And problem here is that Include returns instance of base class, i.e. DbQuery<T> which don't have method Find (lambda-based include is just syntax sugar for calling same string-based include method). And even casting will not help you here, because new instance of base class is created internally:
public DbQuery<TResult> Include(string path)
{
    return new DbQuery<TResult>(this._internalQuery.Include(path));
}

So, use SingleOrDefault to get entity by id
Objective objective = db.Objectives.Include(x => x.Analyst)
                                   .SingleOrDefault(x => x.ObjectiveId == id);

NOTE: One difference here is that Find first checks if entity already exists in context before making database query.

