0
我在LINQ中編寫查詢,但遇到從數據庫獲取正確記錄的問題。當我執行LINQ查詢時,我得到重複的記錄。請參考下面的記錄在JSON格式重複結果:問題從LINQ查詢中獲得正確的結果,請使用LEFT JOIN
[
{
"serverId": 1,
"applicationName": "TestApp",
"resourceName": "Test AppFabric",
"serverName": "Server1",
"aliasName": null,
"os": "Windows Server 2008",
"ipAddress": "192.168.1.1",
"vip": "10.1.1.5",
"url": "www.testapp.com",
"domain": "Domain1",
"network": "Internal Harden",
"typeName": "Application",
"environmentName": "DEV",
"status": "Active"
},
{
"serverId": 1,
"applicationName": "TestApp",
"resourceName": "Test AppFabric",
"serverName": "Server1",
"aliasName": null,
"os": "Windows Server 2008",
"ipAddress": "192.168.1.1",
"vip": "",
"url": "",
"domain": "Domain1",
"network": "Internal Harden",
"typeName": "Application",
"environmentName": "DEV",
"status": "Active"
},
{
"serverId": 2,
"applicationName": "TestApp",
"resourceName": "Test AppFabric",
"serverName": "Server2",
"aliasName": null,
"os": "Windows Server 2008",
"ipAddress": "192.168.1.2",
"vip": "10.1.1.5",
"url": "www.testapp.com",
"domain": "Domain1",
"network": "Internal Harden",
"typeName": "Application",
"environmentName": "DEV",
"status": "Active"
},
{
"serverId": 2,
"applicationName": "TestApp",
"resourceName": "Test AppFabric",
"serverName": "Server2",
"aliasName": null,
"os": "Windows Server 2008",
"ipAddress": "192.168.1.2",
"vip": "",
"url": "",
"domain": "Domain1",
"network": "Internal Harden",
"typeName": "Application",
"environmentName": "DEV",
"status": "Active"
},
{
"serverId": 3,
"applicationName": "TestApp",
"resourceName": "Test AppFabric UI",
"serverName": "Server3",
"aliasName": null,
"os": "Windows Server 2008",
"ipAddress": "172.16.1.1",
"vip": "",
"url": "",
"domain": "Domain2",
"network": "DMZ 1",
"typeName": "Application",
"environmentName": "DEV",
"status": "Active"
},
{
"serverId": 3,
"applicationName": "TestApp",
"resourceName": "Test AppFabric UI",
"serverName": "Server3",
"aliasName": null,
"os": "Windows Server 2008",
"ipAddress": "172.16.1.1",
"vip": "10.1.1.6",
"url": "www.testappui.com",
"domain": "Domain2",
"network": "DMZ 1",
"typeName": "Application",
"environmentName": "DEV",
"status": "Active"
},
{
"serverId": 4,
"applicationName": "TestApp",
"resourceName": "Test AppFabric UI",
"serverName": "Server4",
"aliasName": null,
"os": "Windows Server 2008",
"ipAddress": "172.16.1.2",
"vip": "",
"url": "",
"domain": "Domain2",
"network": "DMZ 1",
"typeName": "Application",
"environmentName": "DEV",
"status": "Active"
},
{
"serverId": 4,
"applicationName": "TestApp",
"resourceName": "Test AppFabric UI",
"serverName": "Server4",
"aliasName": null,
"os": "Windows Server 2008",
"ipAddress": "172.16.1.2",
"vip": "10.1.1.6",
"url": "www.testappui.com",
"domain": "Domain2",
"network": "DMZ 1",
"typeName": "Application",
"environmentName": "DEV",
"status": "Active"
},
{
"serverId": 1002,
"applicationName": "TestApp",
"resourceName": "Test AppFabric",
"serverName": "Server5",
"aliasName": null,
"os": "Windows Server 2008",
"ipAddress": "10.1.1.5",
"vip": "",
"url": "",
"domain": "Domain1",
"network": "Internal Harden",
"typeName": "Application",
"environmentName": "INT",
"status": "Active"
},
{
"serverId": 1003,
"applicationName": "TestApp",
"resourceName": "Test AppFabric",
"serverName": "server6",
"aliasName": null,
"os": "Windows Server 2008",
"ipAddress": "10.1.1.6",
"vip": "",
"url": "",
"domain": "Domain1",
"network": "Internal Harden",
"typeName": "Application",
"environmentName": "INT",
"status": "Active"
}
]
這裏是我用我的ASPT.NET核心應用查詢:
var query = from rg in _context.ResourceGroup
join sr in _context.ServersResourceGroup on rg.Id equals sr.ResourceGroup_id
join rge in _context.ResourceGroupEnvironment on sr.Environment_id equals rge.Environment_id into lrges
from lrge in lrges.Where(r => r.ResourceGroup_id == sr.ResourceGroup_id).DefaultIfEmpty()
join s in _context.Servers on sr.Server_id equals s.Id
join e in _context.Environments on sr.Environment_id equals e.Id
join a in _context.Applications on rg.Application_Id equals a.Id
join d in _context.Domains on s.Domain_Id equals d.Id
join t in _context.Types on rg.Type_Id equals t.Id
join o in _context.OperatingSystems on s.OperatingSystem_Id equals o.Id
join n in _context.NetworkZones on s.NetworkZone_Id equals n.Id
join stat in _context.Status on s.Status.Id equals stat.Id
where a.Name.ToLower() == applicationName.ToLower()
select new SearchListViewModel()
{
serverId = s.Id,
serverName = s.ServerName,
aliasName = s.Alias,
domain = d.Name,
environmentName = e.Name,
network = n.Name,
os = o.OSVersion,
ipAddress = s.IPAddress,
vip = lrge == null ? string.Empty : lrge.VIP,
url = lrge == null ? string.Empty : lrge.EndPointURL,
typeName = t.Name,
applicationName = a.Name,
resourceName = rg.Name,
status = stat.Name
};
return query.ToList();
而且,這裏是原生SQL查詢,只是工作正常。
SELECT s.ServerName, rg.Name as ResourceGroup, e.Name as Env,
rge.EndPointURL, rge.VIP
FROM ResourceGroup as rg
JOIN ServersResourceGroup as srg on rg.Id = srg.ResourceGroup_id
JOIN Servers as s on srg.Server_id = s.Id
JOIN Environments as e on srg.Environment_id = e.Id
LEFT JOIN ResourceGroupEnvironment as rge on srg.Environment_id = rge.Environment_id and rg.Id = rge.ResourceGroup_Id
不知道如果我的LINQ查詢是錯的,但我試圖完成上述在LINQ本地查詢,
這裏是我的實體模型,以幫助瞭解需求:
任何幫助真的很感激。
謝謝
雷
它的工作聯接使用多個條件。我正在尋找幾天來找出如何使用多個條件,現在它是有道理的。非常感謝你。 :) – Ray