Rodrigo Lira
Rodrigo Lira

Reputation: 1589

How do I build a query to select master and last detail (max(Id)) with LiNQ

How do I write a LiNQ to Entities query like the one in the answer of the following question:

SQL Select From Master - Detail Tables

I would like to leverage the existing navigating properties existent in my model (imported database to an Entity Framework EDMX).

Upvotes: 0

Views: 946

Answers (2)

ckal
ckal

Reputation: 3570

In addition to the assumptions AarronLS presented, you'd also need a navigation property from Brands to Models.

var modelsAndBrandsFlattened = from brand in db.Brands
                               let latestModel = brand.Models.Last()
                               select new {
                                   brand.Brand,
                                   brand.BrandId,
                                   latestModel.Model,                                       
                                   latestModel.ModelId
                                }

Upvotes: 1

AaronLS
AaronLS

Reputation: 38394

Assuming there is a navigation property from Model to the Parent Brand called Brand, and db is your DbContext

var modelsAndBrandsFlattened = db.Models.Select(m => new {
        m.Brand.Brand,
        m.Model,
        m.Brand.BrandId,
        m.ModelId
    });

Upvotes: 0

Related Questions