ray mak

ray mak

  • NA
  • 4
  • 1.3k

Issue getting correct results from LINQ query with LEFT JOIN

Nov 14 2016 8:00 AM
Hello,
 
I am writing a query in LINQ, but having issues getting correct record from database. When I execute LINQ query, I get duplicate records. Please see below records with duplicate results:
  1. ServerName ResourceGroup Env EndPointURL VIP  
  2. Server1 Test AppFabric  DEV www.testapp.com 10.1.1.5  
  3. Server1 Test AppFabric  DEV www.testappui.com   10.1.1.6  
  4. Server2 Test AppFabric  DEV www.testapp.com 10.1.1.5  
  5. Server2 Test AppFabric  DEV www.testappui.com   10.1.1.6  
  6. Server3 Test AppFabric UI   DEV www.testapp.com 10.1.1.5  
  7. Server3 Test AppFabric UI   DEV www.testappui.com   10.1.1.6  
  8. Server4 Test AppFabric UI   DEV www.testapp.com 10.1.1.5  
  9. Server4 Test AppFabric UI   DEV www.testappui.com   10.1.1.6  
  10. Server5 Test AppFabric  INT NULL    NULL  
  11. server6 Test AppFabric  INT NULL    NULL  
 Here is the query I am using in my ASPT.NET Core application:
 
  1. var query = from rg in _context.ResourceGroup  
  2.                 join sr in _context.ServersResourceGroup on rg.Id equals sr.ResourceGroup_id  
  3.                 join rge in _context.ResourceGroupEnvironment on sr.Environment_id equals rge.Environment_id into lrges  
  4.                 from lrge in lrges.Where(r => r.ResourceGroup_id == rg.Id).DefaultIfEmpty()  
  5.                 join s in _context.Servers on sr.Server_id equals s.Id  
  6.                 join e in _context.Environments on sr.Environment_id equals e.Id  
  7.                 join a in _context.Applications on rg.Application_Id equals a.Id  
  8.                 join d in _context.Domains on s.Domain_Id equals d.Id  
  9.                 join t in _context.Types on rg.Type_Id equals t.Id  
  10.                 join o in _context.OperatingSystems on s.OperatingSystem_Id equals o.Id  
  11.                 join n in _context.NetworkZones on s.NetworkZone_Id equals n.Id  
  12.                 join stat in _context.Status on s.Status.Id equals stat.Id  
  13.                 where a.Name.ToLower() == applicationName.ToLower()  
  14.                 select new SearchListViewModel()  
  15.                 {  
  16.                     serverId = s.Id,  
  17.                     serverName = s.ServerName,  
  18.                     aliasName = s.Alias,  
  19.                     domain = d.Name,  
  20.                     environmentName = e.Name,  
  21.                     network = n.Name,  
  22.                     os = o.OSVersion,  
  23.                     ipAddress = s.IPAddress,  
  24.                     vip = lrge == null ? string.Empty : lrge.VIP,  
  25.                     url = lrge == null ? string.Empty : lrge.EndPointURL,  
  26.                     typeName = t.Name,  
  27.                     applicationName = a.Name,  
  28.                     resourceName = rg.Name,  
  29.                     status = stat.Name  
  30.                 };  
  31.   
  32.             return query.ToList();  
 
 Also, here is the native SQL query that just works fine.
 
  1. SELECT s.ServerName, rg.Name as ResourceGroup, e.Name as Env,  
  2. rge.EndPointURL, rge.VIP  
  3. FROM ResourceGroup as rg  
  4. JOIN ServersResourceGroup as srg on rg.Id = srg.ResourceGroup_id  
  5. JOIN Servers as s on srg.Server_id = s.Id  
  6. JOIN Environments as e on srg.Environment_id = e.Id  
  7. LEFT JOIN ResourceGroupEnvironment as rge on srg.Environment_id = rge.Environment_id and rg.Id = rge.ResourceGroup_Id  
 Not sure if my LINQ query is wrong, but I am trying to accomplish above native query in LINQ,
 
Here is the linq to entity model http://www.sqlservercentral.com/Forums/Attachment19610.aspx 
 
Any help is really appreciated.
 
Thank you,
 
Ray 
 
 

Answers (2)