I have a model with a hierarchical structure like this:
Batch (root) Groups (List of groups owned by the Batch) Groupnames (Nameing of the Group in different Languages) Tags (List of Tags owned by the Group) Tagnames (Nameing of the Tag in different Languages) Logs (List of Logs owned by the Tag)
I want to display all logs with some fields from its parent Tag and parent Group
plus the names in a selected Language. I have the following LINQ to select the data which produces the results I want, but queries the database 4 times per log.
entities.LOGs.Select(log =>
new
{
GroupType = log.TAG.GROUP.GROUPTYPE, //Additional Querry here
Group = log.TAG.GROUP.GROUPNAMEs.First(gn => gn.LCID == 1031).GROUPNAME1, //Additional Querry here
Tag = log.TAG.TAGNAMEs.First(tn => tn.LCID == 1031).TAGNAME1, //Additional Querry here
Time = log.SERVERTIME,
SourceTime = log.SOURCETIME,
Information = log.INFORMATION,
PreviousValue = log.OLDVALUE,
Value = log.CURRENTVALUE,
Unit = log.TAG.UNIT,//Additional Querry here
Operator = log.OPERATOR
}
);
Is there a way to avoid these additional queries?
EDIT: Here are the querries generated by the above code each subqerry is called 14 times
Main querry:
SELECT
c."GROUPTYPE" AS COL1,
b."GROUPID" AS COL2,
a."TAGID" AS COL3,
a."SERVERTIME" AS COL4,
a."SOURCETIME" AS COL5,
a."INFORMATION" AS COL6,
a."OLDVALUE" AS COL7,
a."CURRENTVALUE" AS COL8,
b."UNIT" AS COL9,
a."OPERATOR" AS COL10
FROM "LOGS" a
LEFT JOIN "TAGS" b
ON (a."TAGID" = b."ID")
LEFT JOIN "GROUPS" c
ON (b."GROUPID" = c."ID")
Subquerry group:
SELECT
"BATCHID" AS COL1,
"BATCHID" AS COL2,
"GROUPTYPE" AS COL3,
"IDENTIFIER" AS COL4,
"SIGNATURE" AS COL5,
"TAGCOUNT" AS COL6
FROM "GROUPS"
WHERE "ID" = CAST(@p0 AS INTEGER)
Subquerry tag:
SELECT
"GROUPID" AS COL1,
"GROUPID" AS COL2,
"IDENTIFIER" AS COL3,
"LOGCOUNT" AS COL4,
"SIGNATURE" AS COL5,
"UNIT" AS COL6
FROM "TAGS"
WHERE "ID" = CAST(@p0 AS INTEGER)
Subquerry groupnames:
SELECT
b."GROUPID" AS COL1,
b."LCID" AS COL2,
b."GROUPID" AS COL3,
b."GROUPNAME" AS COL4,
b."SIGNATURE" AS COL5
FROM "GROUPS" a
LEFT JOIN "GROUPNAMES" b
ON (a."ID" = b."GROUPID")
WHERE a."ID" = CAST(@p0 AS INTEGER)
Subquerry tagnames:
SELECT
b."GROUPID" AS COL1,
b."LCID" AS COL2,
b."GROUPID" AS COL3,
b."GROUPNAME" AS COL4,
b."SIGNATURE" AS COL5
FROM "GROUPS" a
LEFT JOIN "GROUPNAMES" b
ON (a."ID" = b."GROUPID")
WHERE a."ID" = CAST(@p0 AS INTEGER)