I'm trying to get a list of some entity with all sub-entities using LINQ but none of the methods I used works correctly. Here is the classes structure:
public class License {
public virtual Product Product {get; set}
. . . . . .
//some other License properties
. . . . . .
}
public class LicenseUser {
public virtual Account User { get; set;}
public virtual License License { get; set; }
}
I'm trying to get the list of licenses for some user but each License object must contain Product as well. Any query I've tried returned list of license with "null" value in Product property.
Here are the examples LINQ queries what I've tried:
var licenses = context.LicenseUsers
.Include(lu => lu.License.Product)
.Where(lu => lu.User.Id == 1)
.Select(lu => lu.License)
.ToList();
or
var licenses = context.LicenseUsers
.Include(lu => lu.License).ThenInclude(l => l.Product)
.Where(lu => lu.User.Id == 1)
.Select(lu => lu.License)
.ToList();
I'm sure that the records we get by these queries contains valid reference to Products table (ProductId field is not null). I also checked the SQL generated by this LINQ request (using Diagnostic Tools). As expected it does not contains a JOIN statement for Products table.
Is there a correct way to get necessary result?