Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
100 lines (91 sloc) 4.28 KB
using System.Linq;
using System.Threading.Tasks;
using EntityFramework.Demo.Model;
using Microsoft.Extensions.Logging;
namespace EntityFramework.Demo.Demos
{
public class NavigationPropertiesAlternativeQueriesDemo : DemosBase
{
public NavigationPropertiesAlternativeQueriesDemo(DemoDbContext ctx, ILogger logger)
: base(ctx, logger)
{
}
public void UseNavigationalProperty()
{
// Generated query:
//
// SELECT [p].[Id], [p].[Name], [p].[RowVersion], [t0].[Id], [t0].[GroupId], [t0].[Name], [t0].[RowVersion]
// FROM [ProductGroups] AS [p]
// LEFT JOIN (
// SELECT [t].[Id], [t].[GroupId], [t].[Name], [t].[RowVersion]
// FROM (
// SELECT [p0].[Id], [p0].[GroupId], [p0].[Name], [p0].[RowVersion], ROW_NUMBER() OVER(PARTITION BY [p0].[GroupId] ORDER BY [p0].[Name]) AS [row]
// FROM [Products] AS [p0]
// ) AS [t]
// WHERE [t].[row] <= 1
// ) AS [t0] ON [p].[Id] = [t0].[GroupId]
var groups = Context.ProductGroups
.Select(g => new
{
Group = g,
FirstProduct = g.Products.OrderBy(p => p.Name).FirstOrDefault()
})
.ToList();
Print("Loaded using navigational property", groups);
}
public void WithoutNavigationalProperty()
{
// Generated query is the same as above:
//
// SELECT [p].[Id], [p].[Name], [p].[RowVersion], [t0].[Id], [t0].[GroupId], [t0].[Name], [t0].[RowVersion]
// FROM [ProductGroups] AS [p]
// LEFT JOIN (
// SELECT [t].[Id], [t].[GroupId], [t].[Name], [t].[RowVersion]
// FROM (
// SELECT [p0].[Id], [p0].[GroupId], [p0].[Name], [p0].[RowVersion], ROW_NUMBER() OVER(PARTITION BY [p0].[GroupId] ORDER BY [p0].[Name]) AS [row]
// FROM [Products] AS [p0]
// ) AS [t]
// WHERE [t].[row] <= 1
// ) AS [t0] ON [p].[Id] = [t0].[GroupId]
var groups = Context.ProductGroups
.Select(g => new
{
Group = g,
FirstProduct = Context.Products.OrderBy(p => p.Name).FirstOrDefault(p => p.GroupId == g.Id)
})
.ToList();
Print("Loaded without using navigational property", groups);
}
public void WithoutNavigationalPropertyAndWithoutParentDbSet()
{
// Generated query:
//
// SELECT [t].[GroupId], [t1].[Id], [t1].[GroupId], [t1].[Name], [t1].[RowVersion]
// FROM
// (
// SELECT DISTINCT [p].[GroupId]
// FROM [Products] AS [p]
// ) AS [t]
// LEFT JOIN
// (
// SELECT [t0].[Id], [t0].[GroupId], [t0].[Name], [t0].[RowVersion]
// FROM
// (
// SELECT [p0].[Id], [p0].[GroupId], [p0].[Name], [p0].[RowVersion], ROW_NUMBER() OVER(PARTITION BY [p0].[GroupId] ORDER BY [p0].[Name]) AS [row]
// FROM [Products] AS [p0]
// ) AS [t0]
// WHERE [t0].[row] <= 1
// ) AS [t1] ON [t].[GroupId] = [t1].[GroupId]
var groups = Context.Products
.Select(p => p.GroupId)
.Distinct()
.Select(g => new
{
Group = g,
FirstProduct = Context.Products.OrderBy(p => p.Name).FirstOrDefault(p => p.GroupId == g)
})
.ToList();
Print("Loaded without using navigational property and without ProductGroups", groups);
}
}
}
You can’t perform that action at this time.