选择观点的作者

本文关键字:观点 选择 | 更新日期: 2023-09-27 18:13:43

有一个文件:意见。 in Views/Home.

@model Aquapark.ViewModels.OpinionViewModel
@{
    ViewBag.Title = "Opinia";
    Layout = "~/Views/Shared/_StoreLayout.cshtml";
}
<h2>Opinia</h2>
<div style="margin-top: 20px; line-height: 22px;">
    <div class="opinion-box">
        @foreach (var opinions in Model.Opinions)
        {
            <div class="opinion-box-content">
                <div class="opinion-box-content-inside">@opinions.Content</div>
                <div class="date">@opinions.Date</div>
            </div>
            <div class="opinion-author">@opinions.PersonId</div>
        }
    </div>
</div>

我有文件:OpinionViewModel.csViewModel文件夹(创建)。

using Aquapark.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Aquapark.ViewModels
{
    public class OpinionViewModel
    {
        public IEnumerable<Opinion> Opinions { get; set; }
        public IEnumerable<Person> Authors { get; set; }
    }
}

Person.cs

namespace Aquapark.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;
    [Table("Person")]
    public partial class Person
    {
        [Key]
        public int PersonId { get; set; }
        [Required]
        public long Pesel { get; set; }
        [Required]
        [StringLength(30, ErrorMessage = "Imię nie może zawierać więcej niż 30 znaków.")]
        [Display(Name = "Imię")]
        public string FirstName { get; set; }
        [Required]
        [StringLength(60, ErrorMessage = "Nazwisko nie może zawierać wiecej niż 60 znaków.")]
        [Display(Name = "Nazwisko")]
        public string LastName { get; set; }
        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{dd/mm/yyyy}", ApplyFormatInEditMode = true)]
        [Display(Name = "Data urodzenia")]
        public DateTime DateOfBirth  { get; set; }
        public virtual Client Client { get; set; }
        public virtual Employee Employee { get; set; }
        public string FullName
        {
            get
            {
                return FirstName + " " + LastName;
            }
        }
    }
}

HomeController.cs

 public class HomeController : Controller
    {
        private AquaparkContext db = new AquaparkContext();
        // GET: Home
        public ActionResult Index()
        {
            var opinions = db.Opinion.ToList();
            //var randomOpinions = db.Opinion.Where(a => a.Date >= DateTime.Parse("2015-01-01")).OrderBy(g => Guid.NewGuid()).Take(6).ToList();
            var authors = from p in db.Person
                          join o in db.Opinion
                          on p.PersonId equals o.PersonId
                          select new
                          {
                              p.FullName
                          };
            var vm = new OpinionViewModel()
            {
                Opinions = opinions,
                //Authors = authors,
            };
            return View(vm);
        }
        public ActionResult StaticContent(string viewname)
        {
            return View(viewname);
        }

问题:我想显示意见的作者(FirstName和LastName)。我尝试使用LINQ来选择数据(HomeController var authors),但我不能归因于集合authors,因为有一个错误。

错误2不能隐式转换类型"来。这个IQueryable"System.Collections.Generic.IEnumerable"。一个存在显式转换(您是否缺少强制转换?)C:'Users'AsusN551'OneDrive'Documents'Aquapark'Aquapark'Controllers'HomeController.cs 31 27 Aquapark

如何正确解决问题?

选择观点的作者

当您编写代码select new时,您创建了一个新的匿名类型。因为它是一个匿名类型,所以它不知道要匹配哪个值。然而,如果您添加.ToList(),则假定您的类是Ienumerable,这应该与您试图返回的内容相匹配。

除了调用.ToList(),您还可以这样做。

   IEnumerable<Person> authors = (from p in db.Person
                      join o in db.Opinion
                      on p.PersonId equals o.PersonId
                      select new Person
                      {
                          FullName=p.FirstName+p.LastName
                      }).AsEnumerable();
var opinions = db.Opinions
.Select(x => new 
{
    x.Id,
    AuthorFullName = x.Author.FirstName + " " + x.Author.LastName,
    Body = x.Body
})
.ToList();