How can I convert this query from SQL to LINQ

1
 SELECT STUFF((
   SELECT  ','+EXC_Nombre

   FROM Bloqueos as B, Excursiones as E
   Where B.BloqueosId = 3 and E.ExcursionesId IN (
   SELECT 
  [BLE_ExcursionId]      
 FROM [BloqueoExcursions]
 Where BLE_BloqueoID = 3)    
   FOR XML PATH('')
),1,1, '')

This is my database

Table: Excursions

ExcursionesId | EXC_Nombre
--------------|----------
2             | Tangol
3             | estadioo
4             | Visita
5             | Pampa
7             | aa
8             | bbb
21            | cc
22            | mmm

Table: Locks

BloqueosId
----------
69
70
71

Table: BlockingExcursion

BloqueoExcursionId | BLE_ExcurionId | BLE_BloqueoId 
-------------------|----------------|--------------
11                 | 8              | 1 
12                 | 3              | 1 
17                 | 4              | 66
20                 | 2              | 3
21                 | 7              | 3
22                 | 3              | 67 
23                 | 8              | 67 
    
asked by Michael Peñaloza Aguirre 21.09.2017 в 00:44
source

1 answer

1

According to the attached tables - and the results that I understand you expect - I would write the sql query in the following way:

SELECT 
STUFF((SELECT ',' + e.EXC_Nombre 
   FROM BloqueoExcursion be 
      INNER JOIN Excursiones e ON be.BLE_ExcurionId = e.ExcursionesId
   WHERE be.BLE_BloqueoId = 3 GROUP BY e.EXC_Nombre FOR XML PATH('')),1,1, 
'');
GO

Linq inquiry:

string _EXC_Nombre = string.Join(",",
        (from be in dbContext.BloqueoExcursion
         join e in dbContext.Excursiones on be.BLE_ExcurionId equals e.ExcursionesId
         select e.EXC_Nombre).Distinct().ToArray());
    
answered by 21.09.2017 в 05:18