Here is my object model.
public class ItemViewModel
{
public Item ItemObject { get; set; }
public IEnumerable<LineItem> Fields { get; set; } = new List<LineItem>();
}
public class Item
{
public int ItemId { get; set; }
public DateTime DatePurchased { get; set; }
}
public class LineItem
{
public int ItemId { get; set; }
public string Name { get; set; }
}
The database contains Items and LineItems tables, corresponding to the object model. I am using the below written LINQ expression to retrieve an ItemViewModel
object, provided an Item ID.
var i = from i in _dbContext.Items
where i.ItemId == 123
select new ItemViewModel
{
ItemObject = i,
Fields = from field in _dbContext.LineItems
where field.ItemId == i.ItemId
select field
};
var viewModel = await i.SingleOrDefaultAsync();
The code compiles fine but after its execution the viewModel.Fields
property contains only one list item whereas there are three Line Items against ItemID:123, in the database table.
The SQL generated by this snippet (captured through SQL Profiler) returns three line items, through a LEFT JOIN. That means the generated SQL is fine but something is not going well with Entity Framework but I don't know what?
Update: Here is the generated SQL query which correctly gives me three rows:
SELECT
[t].[ItemId], [t].[DatePurchased],
[s0].[ItemId], [s0].[Name]
FROM
(
SELECT
TOP(2) [s].[ItemId], [s].[DatePurchased]
FROM
[Items] AS [s]
WHERE
[s].[ItemId] = 123
) AS [t]
LEFT JOIN [LineItems] AS [s0] ON [t].[ItemId] = [s0].[ItemId]
ORDER BY [t].[ItemId]
Tech stack is .NET Core 3.1 and Entity Framework Core.