Given the following (simplified) classes:
public class Master
{
public int Id { get; set; }
public string UniqueName { get; set; }
public virtual List<Detail> Details { get; set; }
}
public class Detail
{
public int Id { get; set; }
public string UniqueCode { get; set; }
}
How can I select a specific Detail instance given a UniqueName for the Master and a UniqueCode for the Detail using Linq to Entities?
Note that there is no navigation property from Detail to Master.
If there were a navigation property, I think I could do something like this:
Detail detail = (from m in ctx.Master.Include(p=>p.Details)
join d in ctx.Details
on m.Id = d.MasterId
where m.UniqueName == "Something" && d.UniqueCode == "Else"
select d).SingleOrDefault();
Without that navigation property from Detail to Master, I don’t see how to proceed without first fetching the Master including all Detail instances of that Master, and then iterating the Detail instances. That could be quite inefficient if there are many Detail instances for a given Master.
Try