C# LINQ включает VS Navigation, когда присоединится ⇐ C#

Место общения программистов C#
Anonymous
C# LINQ включает VS Navigation, когда присоединится

Сообщение Anonymous »

`Этот пост переведен Ko to eng. сущности не были соединены. < /p>
Почему это? Testtable1 < /p>
testtable12 имеет отношение к одному к одному с Testtable2 < /p>
Вот несколько примеров LINQ: < /p>

Код: Выделить всё

// Eager loading
// Joining one table
var sql = \_context.TestTable12
.Include(d =\> d.TestTable1)
.Where(d =\> d.RoleCode == role)
.OrderBy(d =\> d.TestTable1.Order)
.Select(d =\> new
{
Col1 = d.Col1,
Col2 = d.Menu.Col2,
}).ToQueryString();

// Eager loading
// Joining two tables
var sql2 = \_context.TestTable12
.Include(d =\> d.TestTable1)
.Include(d =\> d.TestTable2)
.Where(d =\> d.RoleCode == role)
.OrderBy(d =\> d.TestTable1.Order)
.Select(d =\> new
{
Col1 = d.Col1,
Col2 = d.TestTable1.Col2,
}).ToQueryString();

// Lazy loading
var sql3 = \_context.TestTable12
.Where(d =\> d.RoleCode == role)
.OrderBy(d =\> d.TestTable1.Order)
.Select(d =\> new
{
Col1 = d.Col1,
Col2 = d.TestTable1.Col2,
}).ToQueryString();
In all these cases, the generated SQL is the same—it only joins TestTable1 and retrieves the necessary columns.

So, my question is:

Why is Include ignored for entities not accessed in Select?

What's the real difference between using .Include() and just using navigation properties directly?

What should I actually use?

// Eager loading
// Joining one table
var sql = \_context.TestTable12
.Include(d =\> d.TestTable1)
.Where(d =\> d.RoleCode == role)
.OrderBy(d =\> d.TestTable1.Order)
.Select(d =\> new
{
Col1 = d.Col1,
Col2 = d.Menu.Col2,
}).ToQueryString();

// Eager loading
// Joining two tables
var sql2 = \_context.TestTable12
.Include(d =\> d.TestTable1)
.Include(d =\> d.TestTable2)
.Where(d =\> d.RoleCode == role)
.OrderBy(d =\> d.TestTable1.Order)
.Select(d =\> new
{
Col1 = d.Col1,
Col2 = d.TestTable1.Col2,
}).ToQueryString();

// Lazy loading
var sql3 = \_context.TestTable12
.Where(d =\> d.RoleCode == role)
.OrderBy(d =\> d.TestTable1.Order)
.Select(d =\> new
{
Col1 = d.Col1,
Col2 = d.TestTable1.Col2,
}).ToQueryString();
Во всех этих случаях сгенерированный SQL одинаков - он только соединяет Test -Testable1 и получает необходимые столбцы.

Подробнее здесь: https://stackoverflow.com/questions/796 ... -when-join

Вернуться в «C#»