我有3个可以通过id加入的XML文件。
var test = from b in doc.Descendants("CalendarFair")
let start = b.Element("DateStart").Value
where
start.Substring(0, 2) == dateNow.Substring(0, 2) &&
start.Substring(6, 4) == dateNow.Substring(6, 4)
let id1 = b.Element("IdExecutive").Value
join c in doc1.Descendants("Executive")
on id1 equals c.Element("Id").Value
select new
{
Title = b.Element("Title").Value,
DateStart=b.Element("DateStart").Value,
DateEnd=b.Element("DateEnd").Value,
NameExecutive = c.Element("NameExecutive").Value,
};
此代码加入CalendarFair
和Executive
现在如何将FairCenter
加入此代码
var test = from b in doc.Descendants("CalendarFair")
let start = b.Element("DateStart").Value
where start.Substring(0, 2) == dateNow.Substring(0, 2) &&
start.Substring(6, 4) == dateNow.Substring(6, 4)
let id1 = b.Element("IdExecutive").Value
join c in doc1.Descendants("Executive")
on id1 equals c.Element("Id").Value
join d in doc3.Descendants("FairCenter")
on ........ //by id
select new
{
Title = b.Element("Title").Value,
DateStart=b.Element("DateStart").Value,
DateEnd=b.Element("DateEnd").Value,
NameExecutive = c.Element("NameExecutive").Value,
telExecutive = c.Element("Tel").Value,
websiteExecutive = c.Element("Website").Value
};
FairCenter.xml
<FairCenters>
<FairCenter>
<Name>c1</Name>
<Id>1</Id>
</FairCenter>
<FairCenter>
<Name>c2</Name>
<Id>1</Id>
</FairCenter>
Executives.xml
<Executives>
<Executive>
<NameExecutive>e1</NameExecutive>
<Id>1</Id>
</Executive>
<Executive>
<NameExecutive>e2</NameExecutive>
<Id>2</Id>
</Executive>
</Executives>
CalendarFair.xml
<CalendarFairs>
<CalendarFair>
<DateStart>09/09/2011</DateStart>
<DateEnd>07/15/2011</DateEnd>
<Title>f1</Title>
<IdExecutive>1</IdExecutive>
<IdCenter>1</IdCenter>
</CalendarFair>
<CalendarFair>
<DateStart>07/14/2011</DateStart>
<DateEnd>07/20/2011</DateEnd>
<Title>f2</Title>
<IdExecutive>5</IdExecutive>
<IdCenter>2</IdCenter>
</CalendarFair>
</CalendarFairs>
答案
var test = from b in doc.Descendants("CalendarFair")
let start = b.Element("DateStart").Value
where start.Substring(0, 2) == dateNow.Substring(0, 2) && start.Substring(6, 4) == dateNow.Substring(6, 4)
let id1 = b.Element("IdExecutive").Value
let id2 = b.Element("IdCenter").Value
join c in doc1.Descendants("Executive")
on id1 equals c.Element("Id").Value
join d in doc3.Descendants("FairCenter")
on id2 equals d.Element("Id").Value
select new
{
Title = b.Element("Title").Value ,
DateStart=b.Element("DateStart").Value,
DateEnd=b.Element("DateEnd").Value,
NameExecutive = c.Element("NameExecutive").Value,
telExecutive = c.Element("Tel").Value,
websiteExecutive = c.Element("Website").Value,
NameCenter = d.Element("Name").Value
};
答案 0 :(得分:2)
在我看来,你只需要:
join d in doc3.Descendants("FairCenter")
on b.Element("IdFair").Value equals d.Element("Id").Value
是什么给你带来了麻烦?请注意,对于连接,您并不需要所有这些let
语句。我认为会更清楚:
var test = from b in doc.Descendants("CalendarFair")
where ...
join c in doc1.Descendants("Executive")
on b.Element("IdExecutive").Value equals c.Element("Id").Value
join d in doc3.Descendants("FairCenter")
on b.Element("IdFair").Value equals d.Element("Id").Value
这清楚地表明你在每个阶段都匹配哪些元素。
我假设您也想在投影中使用d
......
(顺便说一句,你应该看看将DateStart
转换为DateTime
,而不是使用所有这些子字符串操作。)