Assuming that the Stored Proc returns a denormalization of Items
and Users
, one idea comes to mind is to use a projection DTO which mimics the structure of the results of the stored procedure, and then to use the Context.Database.SqlQuery<T>
flavour of SqlQuery
to project into a flattened DTO.
You can then use LINQ
to again re-normalize the result set back into your entity graph, which I guess was the original intention.
In Detail
Assuming the existing entities from your EF Model:
public class Item
{
public int ItemId { get; set; }
public string Name { get; set; }
}
public class User
{
public int UserId { get; set; }
public string Name { get; set; }
public ICollection<Item> Items { get; set; }
}
Create a transfer DTO which represents the flattened structure of your Stored Proc result set:
public class UserItemProcDto
{
public int ItemId { get; set; }
public string ItemName { get; set; }
public int UserId { get; set; }
public string UserName { get; set; }
}
Then project into the denormalized DTO, and finally renormalize using LINQ:
var results = _context.SqlQuery<UserItemProcDto>(query, paramUserId);
var usersWithItems = results.GroupBy(r => r.UserId)
.Select(g => new User
{
UserId = g.Key,
Name = g.First().UserName,
Items = g.Select(i => new Item
{
ItemId = i.ItemId,
Name = i.ItemName
}).ToList()
});
This is obviously not something you would want to do for many stored procedures which span multiple tables, of course :)