简体   繁体   English

C#使用linq将数据选择到视图模型中

[英]C# select data into the viewmodel using linq

I have a viewmodel 我有一个viewmodel

    public class CompanyDetailVM {
      public string CompanyName { get; set; }
      public IEnumerable<Comments> Comments { get; set; }
   }

I want to retrieve all the data coming from XML, I cant figure out how to get list of comments too (Comments model consist of CommentText, DateTaken). 我想检索来自XML的所有数据,我也无法弄清楚如何获取注释列表(Comments模型由CommentText,DateTaken组成)。 My code looks like: 我的代码如下:

   var model= new CompanyDetailVM
   {
       CompanyName = detail.Element("result").Element("name").Value,
       Comments = new Models.Comments {  
                CommentText=  detail.Element("result").Element("comment").Element("text"),
                DateTaken = detail.Element("result").Element("comment").Element("date")
       }
   }

Error: Cannot implicitly convert type 'Models.Comments' to 'System.Collections.Generic.IEnumerable 错误:无法将类型“ Models.Comments”隐式转换为“ System.Collections.Generic.IEnumerable”

I also dont think doing new Models.Comments is the right way. 我也不认为做新的Models.Comments是正确的方法。 How do I fix the code correctly? 如何正确修复代码?

If you just have a single Comment in your XML - as it looks like you do here - then the easiest is to do: 如果您的XML中只有一个Comment(就像您在此处所做的那样),那么最简单的方法是:

var model= new CompanyDetailVM
{
    CompanyName = detail.Element("result").Element("name").Value,
    Comments = new [] { 
        new Models.Comments {  
            CommentText=  detail.Element("result").Element("comment").Element("text"),
            DateTaken = detail.Element("result").Element("comment").Element("date")
        }
    }
 }

(Note the shorthand array literal syntax - new [] { ... } for easily creating an array (which of course implements IEnumerable<> )) (请注意速记数组文字语法-new new [] { ... }用于轻松创建数组(当然实现IEnumerable<> ))

If your XML may contain multiple comments, eg 如果您的XML可能包含多个注释,例如

<result>
    <name>Test</name>
    <comment><text>One</text>...</comment>
    <comment><text>Two</text>...</comment>
</result>

Then you might want to move towards using LinqToXML to transform all <comment> tags into Models.Comments objects. 然后,您可能希望使用LinqToXML将所有<comment>标记转换为Models.Comments对象。

Currently, you assigned a Models.Comment object to a property expecting IEnumerable<Comments> type, which then trigger that conversion error. 当前,您Models.Comment对象分配给期望IEnumerable<Comments>类型的属性,然后触发该转换错误。 You can create IEnumerable<Comments> from XML using LINQ like so : 您可以使用LINQ从XML创建IEnumerable<Comments> ,如下所示:

var model= new CompanyDetailVM
{
   CompanyName = detail.Element("result").Element("name").Value,
   Comments = from comment in detail.Element("result").Elements("comment")
              select new Models.Comments 
                     {
                        CommentText = (string)comment.Element("text"),
                        DateTaken = (DateTime)comment.Element("date")
                     }
}

Notice that I use Elements("comment") (with plural Elements ) assuming that you may have multiple <comment> elements in the XML source. 请注意,假设您在XML源中可能有多个<comment>元素,我使用Elements("comment") (带有多个Elements )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM