gpt4 book ai didi

c# - Linq 表达式 : Left Join Duplicating Rows

转载 作者:行者123 更新时间:2023-11-30 16:51:19 26 4
gpt4 key购买 nike

我正在尝试执行此 Linq 表达式,但 LEFT JOIN 正在复制行。
当我在 SQL 中编写这个查询时,它工作正常,但是当我在 Linq 表达式中编写它时,LEFT JOIN 复制了该行。
我试图分组...但我得到了相同的结果。

var sql = (from project in db.Project
join suitT in db.SuitT
on project.Id equals suitT.IdProject

join inspec in db.Inspec
on suitT.Id equals inspec.IdSuitT

join listFinalDef in db.ListFinalDef
on inspec.Id equals listFinalDef.IdInspec
into myListFinalDef
from groupListFinalDef in myListFinalDef.DefaultIfEmpty()

join artefact1 in db.Artefact
on groupListFinalDef.Id equals artefact1.IdListFinalDef
into myArtefact1
from groupArtefact1 in myArtefact1.DefaultIfEmpty()

join artefact2 in db.Artefact
on inspec.Id equals artefact2.IdInspec
into myArtefact2
from groupArtefact2 in myArtefact2.DefaultIfEmpty()

join typeArtefact in db.TypeArtefact
on inspec.IdTypeArtefact equals typeArtefact.Id
where ...
select new ArtefactModels
{
IdArtefact1 = groupArtefact1.Id,
IdArtefact2 = groupArtefact2.Id,
...,
...,
...
}).ToList();

发生了什么:复制行
示例

Id: 1 | ProjectName: Project 1 | InspecTitle: Title 1
Id: 1 | ProjectName: Project 1 | InspecTitle: Title 1
Id: 2 | ProjectName: Project 2 | InspecTitle: Title 2
Id: 2 | ProjectName: Project 2 | InspecTitle: Title 2

我正在尝试做的事情:
示例

Id: 1 | ProjectName: Project 1 | InspecTitle: Title 1
Id: 2 | ProjectName: Project 2 | InspecTitle: Title 2

已编辑:
我正在尝试执行此 SQL,但使用的是 LINQ 表达式:

SELECT art1.Id AS IdArtefact1, art2.Id AS IdArtefact2, ...
FROM
Project
INNER JOIN SuitT ON Project.Id = SuitT.IdProject
INNER JOIN Inspec ON SuitT.Id = Inspec.IdSuitT
LEFT JOIN ListFinalDef ON Inspec.Id = ListFinalDef.IdInspec
LEFT JOIN Artefact AS art1 ON ListFinalDef.Id = art1.IdListFinalDef
LEFT JOIN Artefact AS art2 ON Inspec.Id = art2.IdInspec
INNER JOIN TypeArtefact ON Inspec.IdTypeArtefact = TypeArtefact.Id
WHERE ...

最佳答案

groupby 是这样的:

var sql = (from project in db.Project
join suitT in db.SuitT
on project.Id equals suitT.IdProject

join inspec in db.Inspec
on suitT.Id equals inspec.IdSuitT

join listFinalDef in db.ListFinalDef
on inspec.Id equals listFinalDef.IdInspec
into myListFinalDef
from groupListFinalDef in myListFinalDef.DefaultIfEmpty()

join artefact1 in db.Artefact
on groupListFinalDef.Id equals artefact1.IdListFinalDef
into myArtefact1
from groupArtefact1 in myArtefact1.DefaultIfEmpty()

join artefact2 in db.Artefato
on inspec.Id equals artefact2.IdInspec
into myArtefact2
from groupArtefact2 in myArtefact2.DefaultIfEmpty()

join typeArtefact in db.TypeArtefact
on inspec.IdTypeArtefact equals typeArtefact.Id
where ...
select new ArtefactModels
{
IdArtefact1 = groupArtefact1.Id,
IdArtefact2 = groupArtefact2.Id,
...,
...,
...
})
//This will return IGroupable<key,value> with as key IdArtefact and as
//value an Enumerable of all the anonymous types with the same IdArtefact
.GroupBy(a => a.IdArtefact1)
//Select first value per IdArtefact if there's multiple
.Select(a => a.FirstOrDefault())
.ToList();

关于c# - Linq 表达式 : Left Join Duplicating Rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34000566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com