社区首页 >问答首页 >如何将IEnumerable<XElement>转换为List<Objects> IActionResult

如何将IEnumerable<XElement>转换为List<Objects> IActionResult
EN

Stack Overflow用户
提问于 2019-06-18 19:05:15
回答 1查看 47关注 0票数 1

我需要返回一个List,但是找不到这样做的方法。这是一个必须返回List<BookItem>的ASP.NET核心Crud方法。在这个方法中,搜索部分工作正常,我想我得到了一个包含字符串的IEnumerable<XElement>列表。

必须将var query转换为IActionResult List<BookItem>

代码语言:javascript
代码运行次数:0
复制
[HttpGet("title/{title}")]
public IActionResult GetBookItemsByTitle(string title)
{
    XDocument doc = _db.GetXmlDb();
    var query = from t in doc.Descendants("book")
                where t.Value.ToLower().Contains(title.ToLower())
                select t.Value;
     // the query  must go into BookItems 
    List<BookItem> BookItems = new List<BookItem>();

    return null;

}

xml如下所示

代码语言:javascript
代码运行次数:0
复制
<?xml version="1.0"?>
<catalog>
  <book id="B1">
    <author>Kutner, Joe</author>
    <title>Deploying with JRuby</title>
    <genre>Computer</genre>
    <price>33.00</price>
    <publish_date>2012-08-15</publish_date>
    <description>Deploying with JRuby is the missing link between enjoying JRuby and using it in the real world to build high-performance, scalable applications.</description>
  </book>
  <book id="B2">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-12-16</publish_date>
    <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
  </book>
  <book id="B3">
    <author>Corets, Eva</author>
    <title>Maeve Ascendant</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-11-17</publish_date>
    <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description>
  </book>
  <book id="B4">
    <author>Corets, Eva</author>
    <title>Oberon's Legacy</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2001-03-10</publish_date>
    <description>In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.</description>
  </book>
  <book id="B5">
    <author>Tolkien, JRR</author>
    <title>The Hobbit</title>
    <genre>Fantasy</genre>
    <price>11.95</price>
    <publish_date>1985-09-10</publish_date>
    <description>If you care for journeys there and back, out of the comfortable Western world, over the edge of the Wild, and home again, and can take an interest in a humble hero blessed with a little wisdom and a little courage and considerable good luck, here is a record of such a journey and such a traveler.</description>
  </book>
  <book id="B6">
    <author>Randall, Cynthia</author>
    <title>Lover Birds</title>
    <genre>Romance</genre>
    <price>4.95</price>
    <publish_date>2000-09-02</publish_date>
    <description>When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.</description>
  </book>
  <book id="B7">
    <author>Thurman, Paula</author>
    <title>Splish Splash</title>
    <genre>Romance</genre>
    <price>4.95</price>
    <publish_date>2000-11-02</publish_date>
    <description>A deep sea diver finds true love twenty thousand leagues beneath the sea.</description>
  </book>
  <book id="B8">
    <author>Knorr, Stefan</author>
    <title>Creepy Crawlies</title>
    <genre>Horror</genre>
    <price>4.95</price>
    <publish_date>2000-12-06</publish_date>
    <description>An anthology of horror stories about roaches, centipedes, scorpions  and other insects.</description>
  </book>
  <book id="B9">
    <author>Kress, Peter</author>
    <title>Paradox Lost</title>
    <genre>Science Fiction</genre>
    <price>6.95</price>
    <publish_date>2000-11-02</publish_date>
    <description>After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.</description>
  </book>
  <book id="B10">
    <author>O'Brien, Tim</author>
    <title>Microsoft .NET: The Programming Bible</title>
    <genre>Computer</genre>
    <price>36.95</price>
    <publish_date>2000-12-09</publish_date>
    <description>Microsoft's .NET initiative is explored in detail in this deep programmer's reference.</description>
  </book>
  <book id="B11">
    <author>Sydik, Jeremy J</author>
    <title>Design Accessible Web Sites</title>
    <genre>Computer</genre>
    <price>34.95</price>
    <publish_date>2007-12-01</publish_date>
    <description>Accessibility has a reputation of being dull, dry, and unfriendly toward graphic design. But there is a better way: well-styled semantic markup that lets you provide the best possible results for all of your users. This book will help you provide images, video, Flash and PDF in an accessible way that looks great to your sighted users, but is still accessible to all users.</description>
  </book>
  <book id="B12">
    <author>Russell, Alex</author>
    <title>Mastering Dojo</title>
    <genre>Computer</genre>
    <price>38.95</price>
    <publish_date>2008-06-01</publish_date>
    <description>The last couple of years have seen big changes in server-side web programming. Now it’s the client’s turn; Dojo is the toolkit to make it happen and Mastering Dojo shows you how.</description>
  </book>
  <book id="B13">
    <author>Copeland, David Bryant</author>
    <title>Build Awesome Command-Line Applications in Ruby</title>
    <genre>Computer</genre>
    <price>20.00</price>
    <publish_date>2012-03-01</publish_date>
    <description>Speak directly to your system. With its simple commands, flags, and parameters, a well-formed command-line application is the quickest way to automate a backup, a build, or a deployment and simplify your life.</description>
  </book>
</catalog>

这是BookItem

代码语言:javascript
代码运行次数:0
复制
public class BookItem
{
    public string Id { get; set; }
    public string Author { get; set; }
    public string Title { get; set; }
    public string Genre { get; set; }
    public decimal Price { get; set; }
    public DateTime Publish_date { get; set; }
    public string Description { get; set; }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-18 19:07:52

您可以尝试使用以下代码吗?我对它进行了测试,它的工作效果与预期相符。

代码语言:javascript
代码运行次数:0
复制
    XDocument doc = _db.GetXmlDb();
    var books = doc.Root.Elements("book")
            .Where(x => x.Element("title").Value.ToLower().Contains(title.ToLower()))
            .Select(x => new BookItem
            {
              Id = x.Attribute("id").Value,
              Author = x.Element("author").Value,
              Title = x.Element("title").Value,
              Genre = x.Element("genre").Value,
              Price = decimal.Parse(x.Element("price").Value.Replace(',', '.'), NumberStyles.Any, CultureInfo.InvariantCulture),
              Publish_date = DateTime.ParseExact(x.Element("publish_date").Value, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None),
              Description = x.Element("description").Value
            }).ToList();


    return Ok(books);

编辑:测试代码。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56655533

复制
相关文章
[1154]如何将字符串转换为datetime
1.把datetime转成字符串: 2017-11-23 17:05:18 2.把字符串转成datetime: 2017-11-23 16:10:10 3.把字符串转成时间戳形式: 1511424610.0 4.把时间戳转成字符串形式: 2017-11-23 17:05:18 5.把datetime类型转外时间戳形式: 1511427918.0
周小董
2022/07/27
3.3K0
pandas datetime与时间戳互相转换,字符串转换datetime
to_datetime 如果传入的是10位时间戳,unit设置为秒,可以转换为datetime
lovelife110
2021/12/08
4.7K0
如何将 Python datetime.datetime 转换为 Excel 序列号?
Excel 使用一种特殊格式来存储日期和时间,称为序列号。序列号是自 1 年 1899 月 <> 日(Excel 认为是时间开始的日期)以来的天数。
很酷的站长
2023/08/11
3440
如何将 Python datetime.datetime 转换为 Excel 序列号?
将字符串转换为date类型_java字符串转date类型
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
全栈程序员站长
2022/11/09
14.1K0
Pandas DateTime 超强总结
对于 Pandas 来说,可以处理众多的数据类型,其中最有趣和最重要的数据类型之一就是时间序列数据。时间序列数据无处不在,它在各个行业都有很多应用。患者健康指标、股票价格变化、天气记录、经济指标、服务器、网络、传感器和应用程序性能监控都是时间序列数据的应用方向
周萝卜
2022/05/22
5.6K0
Pandas DateTime 超强总结
将tensor转换为图像_tensor转int
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
全栈程序员站长
2022/11/07
11.4K0
mysql整型转字符串_java中如何将字符串转换为字符数组
select * from A order by cast(name as unsigned);
全栈程序员站长
2022/09/27
23.3K0
如何将Pandas数据转换为Excel文件
将数据导出到Excel文件通常是任何用户阅读和解释一组数据的最优先和最方便的方式。通过使用Pandas库,可以用Python代码将你的网络搜刮或其他收集的数据导出到Excel文件中,而且步骤非常简单。
玖柒的小窝
2021/11/01
7.6K0
java将字符串转换为json对象的方法_java jsonobject转string
在与服务器交互的时候,我们往往会使用json字符串,今天的例子是java对象转化为字符串,
全栈程序员站长
2022/11/08
21.2K0
Python将字符串转换为列表
We can convert a string to list in Python using split() function.
全栈程序员站长
2022/09/06
6K0
将 Pandas 换为交互式表格的 Python 库
Pandas是我们日常处理表格数据最常用的包,但是对于数据分析来说,Pandas的DataFrame还不够直观,所以今天我们将介绍4个Python包,可以将Pandas的DataFrame转换交互式表格,让我们可以直接在上面进行数据分析的操作。
用户6888863
2023/09/06
2560
将 Pandas 换为交互式表格的 Python 库
将 Pandas 换为交互式表格的 Python 库
Pivottablejs是一个通过IPython widgets集成到Python中的JavaScript库,允许用户直接从DataFrame数据创建交互式和灵活的汇总报表。可以进行高效、清晰的数据分析和表示,帮助将数据从Pandas DataFrame转换为易于观察的交互式数据透视表。
数据STUDIO
2023/09/04
2270
将 Pandas 换为交互式表格的 Python 库
将 Pandas 换为交互式表格的 Python 库
Pandas是我们日常处理表格数据最常用的包,但是对于数据分析来说,Pandas的DataFrame还不够直观,所以今天我们将介绍4个Python包,可以将Pandas的DataFrame转换交互式表格,让我们可以直接在上面进行数据分析的操作。
Python数据科学
2023/08/29
1950
将 Pandas 换为交互式表格的 Python 库
在Python如何将 JSON 转换为 Pandas DataFrame?
在数据处理和分析中,JSON是一种常见的数据格式,而Pandas DataFrame是Python中广泛使用的数据结构。将JSON数据转换为Pandas DataFrame可以方便地进行数据分析和处理。在本文中,我们将探讨如何将JSON转换为Pandas DataFrame,并介绍相关的步骤和案例。
网络技术联盟站
2023/08/03
1.2K0
在Python如何将 JSON 转换为 Pandas DataFrame?
sql server 字符串转成日期格式_sql datetime转字符串
2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值
全栈程序员站长
2022/11/09
2.3K0
UTC时间格式转换为DateTime,Python
调用接口的时候传递了一个时间参数,大概就是长这样的:2020-07-22T02:26:37.329Z
SingYi
2022/07/14
2.5K0
UTC时间格式转换为DateTime,Python
JavaSE-将字符串转换为数字
提示:仔细思考所有可能的输入情况。这个问题没有给出输入的限制,你需要自己考虑所有可能的情况。
程序员阿杜
2021/03/15
2.5K0
JavaSE-将字符串转换为数字
JavaSE-将字符串转换为数字
提示:仔细思考所有可能的输入情况。这个问题没有给出输入的限制,你需要自己考虑所有可能的情况。
程序员阿杜
2021/04/07
2.4K0
date转换为localdatetime_java datetime
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
全栈程序员站长
2022/11/10
6000
点击加载更多

相似问题

DTS无法将datetime2转换为datetime

14

Pandas:将字符串转换为datetime.datetime

160

使用Pandas将字符串转换为datetime

17

无法使用pandas to_datetime方法将字符串转换为datetime数据类型

213

我无法将Pandas Dataframe类型转换为datetime

10
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文