[英]LINQ to XML join issue
I'm having real trouble with a join that I am trying to perform on two separate XML files. 我在尝试对两个单独的XML文件执行联接时遇到了真正的麻烦。
I have two classes that describe each object that I extract from the XML files, they are 我有两个类描述从XML文件提取的每个对象,它们是
public class XMLCustomers
{
public String CustomerID { get; set; }
public String ContactName { get; set; }
}
and 和
public class XMLOrders
{
public String OrderID { get; set; }
public String CustomerID { get; set; }
public String OrderDate { get; set; }
public String ShippedDate { get; set; }
public String Freight { get; set; }
public String ShipName { get; set; }
public String ShipCity { get; set; }
public String ShipCountry { get; set; }
}
And my last class stores the join that I perform on the two sets of data. 我的上一课存储了我在两组数据上执行的联接。
public class PostXMLJoinOrder
{
public String OrderID { get; set;}
public String ContactName { get; set;}
public String OrderDate { get; set;}
public String ShippedDate { get; set;}
public String Freight { get; set;}
public String ShipName { get; set;}
public String ShipCity { get; set;}
public String ShipCountry { get; set;}
}
Finally these are my two methods that load the information from the XML files and the third method performs a join and stores the information in a IEnumerable 最后,这是我的两个方法,它们从XML文件中加载信息,第三个方法执行联接并将信息存储在IEnumerable中。
public IEnumerable<XMLCustomers> LoadXMLCustomers() {
var custs = from x in XElement.Load(System.Web.HttpContext.Current.Server.MapPath(@"~\App_Data\XCustomers.xml")).Elements()
select new XMLCustomers
{
ContactName = x.Attribute("ContactName").Value,
CustomerID = x.Attribute("CustomerID").Value
};
int size = custs.Count();
return custs;
}
public IEnumerable<XMLOrders> LoadXMLOrders() {
var ords = from x in XElement.Load(System.Web.HttpContext.Current.Server.MapPath(@"~\App_Data\XOrders.xml")).Elements()
select new XMLOrders
{
OrderID = x.Attribute("ContactName").Value,
CustomerID = x.Attribute("CustomerID").Value,
OrderDate = x.Attribute("OrderDate").Value,
ShippedDate = x.Attribute("ShippedDate").Value,
Freight = x.Attribute("Freight").Value,
ShipName = x.Attribute("ShipName").Value,
ShipCity = x.Attribute("ShipCity").Value,
ShipCountry = x.Attribute("ShipCountry").Value
};
int size = ords.Count();
return ords;
}
public IEnumerable<PostXMLJoinOrder> LoadPostXMLJoinOrders() {
var joinQuery = from customer in LoadXMLCustomers()
from orders in LoadXMLOrders()
where customer.CustomerID == orders.CustomerID
select new PostXMLJoinOrder
{
OrderID = orders.OrderID,
ContactName = customer.ContactName,
OrderDate = orders.OrderDate,
ShippedDate = orders.ShippedDate,
Freight = orders.Freight,
ShipName = orders.ShipName,
ShipCity = orders.ShipCity,
ShipCountry = orders.ShipCountry
};
return joinQuery;
}
I have tested the amount of items returned from my LINQ and it continues to be 0; 我已经测试了从LINQ返回的项目数量,该数量仍为0; I have double checked everything being loaded in from the files but I cant seem to get my head around at which point its going wrong.
我已经仔细检查了文件中正在加载的所有内容,但似乎无法解决问题。
Edit: its a loading issue. 编辑:其加载问题。 the XML files are definitely stored under the correct App_Data folder.
XML文件肯定存储在正确的App_Data文件夹下。 But when the individial LoadXMLCustomers() is running Im getting a NullReferenceException at the part where the lINQ statement selects and creates the new Loaded Customer object.
但是,当个别LoadXMLCustomers()运行时,我在lINQ语句选择并创建新的Loaded Customer对象的部分得到了NullReferenceException。
I have already made sure the build for the XML documents are content and copyToOutputDirectory is set to if newer 我已经确保XML文档的构建内容正确并且将copyToOutputDirectory设置为较新的版本
this is the Exception & Also the var value is null so its definitely not loading for some reason: 这是一个例外,而且var值是null,因此由于某些原因,它肯定不会加载:
SOLVED: The lesson I learnt here is that you need to pay close attention to your XML and data. 解决:我在这里学到的教训是,您需要密切注意XML和数据。 If some of your XML data has empty values, then you need to account by making sure the select statement can handle it.
如果您的某些XML数据具有空值,则需要通过确保select语句可以处理它来进行说明。
Above I had the code 上面我有代码
select new XMLOrders
{
OrderID = x.Attribute("ContactName").Value,
CustomerID = x.Attribute("CustomerID").Value,
OrderDate = x.Attribute("OrderDate").Value,
ShippedDate = x.Attribute("ShippedDate").Value,
Freight = x.Attribute("Freight").Value,
ShipName = x.Attribute("ShipName").Value,
ShipCity = x.Attribute("ShipCity").Value,
ShipCountry = x.Attribute("ShipCountry").Value
};
Which really should have included casts to string values so if there is empty data is "" not throwing a null exception. 实际上应该包括对字符串值的强制转换,因此,如果数据为空,则不会抛出null异常。 Secondly I should have been getting the Element values not the Attribute values.
其次,我应该获取元素值而不是属性值。
select new XMLOrders
{
OrderID = (string)x.Element("OrderID").Value,
CustomerID = (string)x.Element("CustomerID").Value,
OrderDate = (string)x.Element("OrderDate").Value,
ShippedDate = (string)x.Element("ShippedDate").Value,
Freight = (string)x.Element("Freight").Value,
ShipName = (string)x.Element("ShipName").Value,
ShipCity = (string)x.Element("ShipCity").Value,
ShipCountry = (string)x.Element("ShipCountry").Value
};
Have you tried not to use join
but just: 您是否尝试过不使用
join
而是:
from customer in LoadXMLCustomers()
from order in LoadXMLOrders()
where customer.CustomerID = order.CustomerID
select new PostXMLJoinOrder
...
Just notice that in such query LoadXMLOrders()
can be called once again for each customer. 请注意,在这样的查询中,可以为每个客户再次调用
LoadXMLOrders()
。 So pre-save it first. 因此,请先预先保存。
Also don't forget to materialize queries calling ToArray()
/ ToList()
. 同样不要忘记实现调用
ToArray()
/ ToList()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.