The above query, if it is written directly in SQL, will be written like this (pseudocode):
SELECT X.NAME AS NAME, (CASE R.TYPE WHEN A THEN R ELSE NULL) AS TypeARelated, (CASE R.TYPE WHEN B THEN R ELSE NULL) AS TypeBRelated FROM Items AS X JOIN Related AS R ON <some field>
However, linq-to-sql is not so efficient, from your explanation, it is combined, and then it goes to individually compare each record. A better way would be to use two linq queries, similar to your first example, which will generate two SQL queries. Then use the result of the two linq queries and attach them to them, which will not generate the SQL query. This method would limit the number of queries executed in SQL to 2.
If the number of conditions ierType == "A", etc. over time, various conditions will increase or various conditions will be added; you are better off using a stored procedure, which will be a single SQL query at any time.
Hasanain
source share