我正在使用Linq解析xml文档,但我遇到了一个我不确定如何处理处理的场景。 我发现/尝试过的所有东西似乎都不起作用。 正如你在下面看到的(非常简单的场景),OrderAmount是空的。 这是一个十进制字段。 当我尝试在LINQ中处理这个问题时,它不断轰炸。 我曾尝试使用deimcal ?,使用空检查等等,似乎没有任何工作(最有可能的用户错误)。 任何建议,将不胜感激。
当一个元素是一个非字符串(如十进制)并且为空时,如何处理?
XML:
<Orders> <Order> <OrderNumber>12345</OrderNumber> <OrderAmount/> </Order> </Orders>LINQ:
List<Order> myOrders = from orders in xdoc.Descendants("Order") select new Order{ OrderNumber = (int)orders.Element("OrderNumber"), OrderAmount = (decimal?)orders.Element("OrderAmount"), }.ToList<Order>();I am parsing an xml doc with Linq, but i am running into a scenario where I am not sure how to handle the processing. Everything I have found / tried doesn't seem to work. As you can see below (very simple scenario) where the OrderAmount is empty. This is a decimal field. When I try to handle this in LINQ it keeps bombing. I have tried using deimcal?, using the null check, etc nothing seems to be working (Most likely user error). Any suggestions would be appreciated.
How do you handle when an element is a non string (like a decimal) and is empty ?
Xml:
<Orders> <Order> <OrderNumber>12345</OrderNumber> <OrderAmount/> </Order> </Orders>LINQ:
List<Order> myOrders = from orders in xdoc.Descendants("Order") select new Order{ OrderNumber = (int)orders.Element("OrderNumber"), OrderAmount = (decimal?)orders.Element("OrderAmount"), }.ToList<Order>();最满意答案
将XElement为decimal? 仅当元素实际为null时才返回null 。
我认为最可读的解决方案是这样的:
elem.IsEmpty ? null : (decimal?)elem如果您经常使用它,您可能需要将其放入扩展方法中。 或者只是在你的LINQ查询中使用let不要重复选择元素的代码。
from orders in xdoc.Descendants("Order") let amountElem = orders.Element("OrderAmount") select new Order { OrderNumber = (int)orders.Element("OrderNumber"), OrderAmount = amountElem.IsEmpty ? null : (decimal?)amountElem }另一种选择是,如果您可以更改XML,则只是简单地省略表示null的元素。 它应该与你已有的代码一起工作。
编辑:扩展方法看起来像这样:
static class XElementExtensions { public static decimal? ToNullableDecimal(this XElement elem) { return elem.IsEmpty ? null : (decimal?)elem; } }你会这样使用它:
OrderAmount = orders.Element("OrderAmount").ToNullableDecimal()The cast of XElement to decimal? returns null only when the element is actually null.
I think the most readable solution is something like this:
elem.IsEmpty ? null : (decimal?)elemYou might want to put this into an extension method, if you use it often. Or just use let in your LINQ query to not repeat the code that selects the element.
from orders in xdoc.Descendants("Order") let amountElem = orders.Element("OrderAmount") select new Order { OrderNumber = (int)orders.Element("OrderNumber"), OrderAmount = amountElem.IsEmpty ? null : (decimal?)amountElem }Another option, if you can change the XML, is simply omitting the element to represent null. It should work well with the code you already have.
EDIT: The extension method would look something like this:
static class XElementExtensions { public static decimal? ToNullableDecimal(this XElement elem) { return elem.IsEmpty ? null : (decimal?)elem; } }And you would use it like this:
OrderAmount = orders.Element("OrderAmount").ToNullableDecimal()Linq to Xml - 空元素(Linq to Xml - Empty Element)我正在使用Linq解析xml文档,但我遇到了一个我不确定如何处理处理的场景。 我发现/尝试过的所有东西似乎都不起作用。 正如你在下面看到的(非常简单的场景),OrderAmount是空的。 这是一个十进制字段。 当我尝试在LINQ中处理这个问题时,它不断轰炸。 我曾尝试使用deimcal ?,使用空检查等等,似乎没有任何工作(最有可能的用户错误)。 任何建议,将不胜感激。
当一个元素是一个非字符串(如十进制)并且为空时,如何处理?
XML:
<Orders> <Order> <OrderNumber>12345</OrderNumber> <OrderAmount/> </Order> </Orders>LINQ:
List<Order> myOrders = from orders in xdoc.Descendants("Order") select new Order{ OrderNumber = (int)orders.Element("OrderNumber"), OrderAmount = (decimal?)orders.Element("OrderAmount"), }.ToList<Order>();I am parsing an xml doc with Linq, but i am running into a scenario where I am not sure how to handle the processing. Everything I have found / tried doesn't seem to work. As you can see below (very simple scenario) where the OrderAmount is empty. This is a decimal field. When I try to handle this in LINQ it keeps bombing. I have tried using deimcal?, using the null check, etc nothing seems to be working (Most likely user error). Any suggestions would be appreciated.
How do you handle when an element is a non string (like a decimal) and is empty ?
Xml:
<Orders> <Order> <OrderNumber>12345</OrderNumber> <OrderAmount/> </Order> </Orders>LINQ:
List<Order> myOrders = from orders in xdoc.Descendants("Order") select new Order{ OrderNumber = (int)orders.Element("OrderNumber"), OrderAmount = (decimal?)orders.Element("OrderAmount"), }.ToList<Order>();最满意答案
将XElement为decimal? 仅当元素实际为null时才返回null 。
我认为最可读的解决方案是这样的:
elem.IsEmpty ? null : (decimal?)elem如果您经常使用它,您可能需要将其放入扩展方法中。 或者只是在你的LINQ查询中使用let不要重复选择元素的代码。
from orders in xdoc.Descendants("Order") let amountElem = orders.Element("OrderAmount") select new Order { OrderNumber = (int)orders.Element("OrderNumber"), OrderAmount = amountElem.IsEmpty ? null : (decimal?)amountElem }另一种选择是,如果您可以更改XML,则只是简单地省略表示null的元素。 它应该与你已有的代码一起工作。
编辑:扩展方法看起来像这样:
static class XElementExtensions { public static decimal? ToNullableDecimal(this XElement elem) { return elem.IsEmpty ? null : (decimal?)elem; } }你会这样使用它:
OrderAmount = orders.Element("OrderAmount").ToNullableDecimal()The cast of XElement to decimal? returns null only when the element is actually null.
I think the most readable solution is something like this:
elem.IsEmpty ? null : (decimal?)elemYou might want to put this into an extension method, if you use it often. Or just use let in your LINQ query to not repeat the code that selects the element.
from orders in xdoc.Descendants("Order") let amountElem = orders.Element("OrderAmount") select new Order { OrderNumber = (int)orders.Element("OrderNumber"), OrderAmount = amountElem.IsEmpty ? null : (decimal?)amountElem }Another option, if you can change the XML, is simply omitting the element to represent null. It should work well with the code you already have.
EDIT: The extension method would look something like this:
static class XElementExtensions { public static decimal? ToNullableDecimal(this XElement elem) { return elem.IsEmpty ? null : (decimal?)elem; } }And you would use it like this:
OrderAmount = orders.Element("OrderAmount").ToNullableDecimal()
发布评论