嵌套导航属性+生成的SQL查询

时间:2016-12-31 05:30:08

标签: c# entity-framework linq

我想知道以下生成的查询是否能以某种方式得到改进。

我有以下linq表达式:

var gps = DBContext.GPS.Where(x => x.ID == id)
    .Select(c => new
    {
        Id = c.ID,
        Descripcion = c.DESCRIPCION_LARGA,
        Alarmas = c.ALARMAS.Select(a => new
        {
            FechaHora = a.FECHAHORA,
            CercaDescripcion = a.CERCA.DESCRIPCION_LARGA
        })
    });

ALARMAS是GPS实体的导航属性。这会生成以下sql查询:

SELECT "Project1"."ID"           AS "ID",
    "Project1"."DESCRIPCION_LARGA"  AS "DESCRIPCION_LARGA",
    "Project1"."C1"                 AS "C1",
    "Project1"."GPS_ID"             AS "GPS_ID",
    "Project1"."FECHAHORA"          AS "FECHAHORA",
    "Project1"."DESCRIPCION_LARGA1" AS "DESCRIPCION_LARGA1"
FROM
    (SELECT "Extent1"."ID"         AS "ID",
        "Extent1"."DESCRIPCION_LARGA" AS "DESCRIPCION_LARGA",
        "Join1"."GPS_ID"              AS "GPS_ID",
        "Join1"."FECHAHORA"           AS "FECHAHORA",
        "Join1"."DESCRIPCION_LARGA"   AS "DESCRIPCION_LARGA1",
        CASE
            WHEN ("Join1"."GPS_ID" IS NULL)
            THEN NULL
            ELSE 1
        END AS "C1"
    FROM "GPS" "Extent1"
    LEFT OUTER JOIN
        (SELECT "Extent2"."GPS_ID"     AS "GPS_ID",
            "Extent2"."ALARMA_ID"         AS "ALARMA_ID",
            "Extent2"."LOG_ID"            AS "LOG_ID",
            "Extent2"."CERCA_ID"          AS "CERCA_ID",
            "Extent2"."FECHAHORA"         AS "FECHAHORA",
            "Extent3"."ID"                AS "ID",
            "Extent3"."DESCRIPCION_LARGA" AS "DESCRIPCION_LARGA",
            "Extent3"."DESCRIPCION_MED"   AS "DESCRIPCION_MED",
            "Extent3"."DESCRIPCION_CORTA" AS "DESCRIPCION_CORTA",
            "Extent3"."ABREVIACION"       AS "ABREVIACION",
            "Extent3"."CAPA_ID"           AS "CAPA_ID",
            "Extent3"."CATEGORIA_ID"      AS "CATEGORIA_ID",
            "Extent3"."PADRE_ID"          AS "PADRE_ID",
            "Extent3"."REGION_ID"         AS "REGION_ID",
            "Extent3"."FORMA_ID"          AS "FORMA_ID",
            "Extent3"."VERTICE1_LAT"      AS "VERTICE1_LAT",
            "Extent3"."VERTICE1_LNG"      AS "VERTICE1_LNG",
            "Extent3"."VERTICE2_LAT"      AS "VERTICE2_LAT",
            "Extent3"."VERTICE2_LNG"      AS "VERTICE2_LNG",
            "Extent3"."LAT"               AS "LAT",
            "Extent3"."LNG"               AS "LNG",
            "Extent3"."RADIO"             AS "RADIO",
            "Extent3"."ESTADO_ID"         AS "ESTADO_ID",
            "Extent3"."CERCA_ENRUTA_ID"   AS "CERCA_ENRUTA_ID",
            "Extent3"."OBSERVACIONES"     AS "OBSERVACIONES"
        FROM "GPS_ALARMAS" "Extent2"
        LEFT OUTER JOIN "CERCAS" "Extent3"
        ON "Extent2"."CERCA_ID"     = "Extent3"."ID"
        ) "Join1" ON "Extent1"."ID" = "Join1"."GPS_ID"
    WHERE ("Extent1"."ID"        = :p__linq__0)
    ) "Project1"
ORDER BY "Project1"."ID" ASC,
    "Project1"."C1" ASC

我的问题基本上是为什么当只需要ID和DESCRIPCION_LARGA时查询表CERCA(Extend3)上的所有列?

这可以改善吗?或者我不应该担心它?

先致谢谢,并致以诚挚的问候, 费边

0 个答案:

没有答案