对 XAttribute 值进行显式转换
我最近写了一段代码,看起来有点像这样:
IEnumerable<DTO.Employee> xEmployee =
from e in xDoc.Descendants("Employee")
where int.Parse(e.Attribute("Id").Value) == emp.Id
select new DTO.Employee
{
Id = (int)e.Attribute("Id"),
LastName = (string)e.Element("LastName"),
FirstName = (string)e.Element("FirstName"),
Email = (string)e.Element("Email")
};
但是,我对 where 子句中转换为 int 感到困惑。首先,我写了一些
where (int)(e.Attribute("Id").Value) == emp.Id
无法编译的东西。 为什么我可以对 (e.Attribute("Id")) 进行显式转换,但不能对 (e.Attribute("Id").Value) 执行此操作?
I recently wrote a piece of code that looked a bit like this:
IEnumerable<DTO.Employee> xEmployee =
from e in xDoc.Descendants("Employee")
where int.Parse(e.Attribute("Id").Value) == emp.Id
select new DTO.Employee
{
Id = (int)e.Attribute("Id"),
LastName = (string)e.Element("LastName"),
FirstName = (string)e.Element("FirstName"),
Email = (string)e.Element("Email")
};
However, I am confused about the casting to an int in the where clause. First, I'd written something like
where (int)(e.Attribute("Id").Value) == emp.Id
which didn't compile.
Why can I do a explicit cast on (e.Attribute("Id")), but can 't I do this on (e.Attribute("Id").Value)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 XAttribute< 的显式运算符重载/a> 类。
Check out the explicit operator overloads of the XAttribute class.
有一个 从
XAttribute
到的显式转换int
- 但没有从string
(XAttribute.Value
的类型)到int
的显式转换。There is an explicit conversion from
XAttribute
toint
- but there's no explicit conversion fromstring
(the type ofXAttribute.Value
) toint
.