Reputation: 5167
I have the following LINQ query which selects some Navigation Property's properties.
var nodes = await dbContext.MonProfiles
.Include(x => x.Nodes)
.SelectMany(x =>
x.Nodes.Select(y =>
new {y.NodeNativeId, y.NodeClassId, y.NodeName, y.NodeClass.ClassName}))
.ToListAsync();
I would like to also be able to select another Navigation's Property's properties so that it is a single round trip to the database. Below is what I have tried, but it did not compile:
var nodes = await dbContext.MonProfiles
.Include(x => x.Nodes)
.SelectMany(x =>
new
{
x.Nodes.Select(y =>
new {y.NodeNativeId, y.NodeClassId, y.NodeName, y.NodeClass.ClassName}),
x.CasAttributes.Select(y => new {y.AttributeName})
})
.ToListAsync();
Upvotes: 0
Views: 388
Reputation: 1079
Your description was a little unclear, but I think what you want is something like the following:
var nodes = await (from profile in dbContext.MonProfiles.Include(x => x.Nodes)
select new {
Nodes = from node in profile.Nodes
select new {
node.NodeNativeId,
node.NodeClassId,
node.NodeName,
node.NodeClassName }
CasAttributes = from attribute in profile.CasAttributes
select attribute.Name
}).ToListAsync();
I'm not sure if this will work with your specific database provider, but the issue with the code you provided was the anonymous type didn't know what name to use for the fields. Sometimes it can infer it, if it's simple enough (for example, just accessing a property), but when you call a Select statement or something you have to specify the names of the fields.
Upvotes: 1