我的请求有问题,我搜索了他们的文件
有桌子
EQUIPEMENT:
+----------+---------+----+
| PMRQTOTM | LIBELLE | TR |
+----------+---------+----+
| 1234 | POMPE 3 | 1 |
| 1245 | POMPE 3 | 2 |
| 1145 | POMPE 4 | 2 |
+----------+---------+----+
文件:
+----------+------+-------+
| PMRQTOTM | TYPE | REF |
+----------+------+-------+
| 1234 | ADR | 5647 |
| 1234 | DSI | 5748 |
| 1145 | ADR | 4758 |
+----------+------+-------+
模特:
public class CapitalisationViewModel
{
public string PMRQTOTM { get; set; }
public string LIBELLE { get; set; }
public int TR { get; set; }
public List<DOCUMENT> Docs {get; set;}
}
最后马林克的要求:
from capi in northwind.CAPITALISATION
from documents in northwind.DOCUMENT.Where(documents => documents.PMRQTOTM == capi.PMRQTOTM).DefaultIfEmpty()
// <== left join
select new
{
capi.PMRQTOTM,
capi.LIBELLE,
capi.TR,
documents.TYPE,
documents.REF,
})
如何填写List<DOCUMENT>
?
答案 0 :(得分:1)
返回主/详细结果集时不需要左连接。只需遵循“自然”的方式:
var result =
(from capi in northwind.CAPITALISATION
select new CapitalisationViewModel
{
PMRQTOTM = capi.PMRQTOTM,
LIBELLE = capi.LIBELLE,
TR = capi.TR,
Docs = northwind.DOCUMENT.Where(documents => documents.PMRQTOTM == capi.PMRQTOTM).ToList()
})
.ToList();
如果您在实体模型中定义了正确的导航属性,则甚至不需要考虑连接。例如,通常您的Documents
实体中会有一个导航属性CAPITALISATION
,因此上述内容很简单
var result =
(from capi in northwind.CAPITALISATION
select new CapitalisationViewModel
{
PMRQTOTM = capi.PMRQTOTM,
LIBELLE = capi.LIBELLE,
TR = capi.TR,
Docs = capi.Documents.ToList()
})
.ToList();
答案 1 :(得分:0)
这是一个例子,它是不言自明的:
var capLst = new List<CapitalisationViewModel>();
var cap1 = new CapitalisationViewModel();
cap1.PMRQTOTM = 1234;
cap1.LIBELLE = "POMPE 3";
cap1.TR = 1;
var cap2 = new CapitalisationViewModel();
cap2.PMRQTOTM = 1245;
cap2.LIBELLE = "POMPE 3";
cap2.TR = 2;
var cap3 = new CapitalisationViewModel();
cap3.PMRQTOTM = 1145;
cap3.LIBELLE = "POMPE 4";
cap3.TR = 2;
capLst.Add(cap1);
capLst.Add(cap2);
capLst.Add(cap3);
var docLst = new List<DOCUMENT>();
var doc1 = new DOCUMENT();
doc1.PMRQTOTM = 1234;
doc1.TYPE = "ADR";
doc1.REF = 5647;
var doc2 = new DOCUMENT();
doc2.PMRQTOTM = 1234;
doc2.TYPE = "DSI ";
doc2.REF = 5748;
var doc3 = new DOCUMENT();
doc3.PMRQTOTM = 1145;
doc3.TYPE = "ADR";
doc3.REF = 4758;
var doc4 = new DOCUMENT();
doc4.PMRQTOTM = 1145;
doc4.TYPE = "ADR";
doc4.REF = 4758;
docLst.Add(doc1);
docLst.Add(doc2);
docLst.Add(doc3);
docLst.Add(doc4);
//left join
var res = (from cap in capLst
join
doc in docLst
on cap.PMRQTOTM equals doc.PMRQTOTM
into grouping
from doc in grouping.DefaultIfEmpty()
select new
{
cap.PMRQTOTM,
doc
}).ToList();
//Capitalisation with Doc List
var result =
(from capi in capLst
select new CapitalisationViewModel
{
PMRQTOTM = capi.PMRQTOTM,
LIBELLE = capi.LIBELLE,
TR = capi.TR,
Docs = docLst.Where(documents => documents.PMRQTOTM == capi.PMRQTOTM).ToList()
})
.ToList();