在VS 2017到VS 2019升级之后,为什么不按数据库表中计算列对查询进行排序

时间:2019-11-07 18:05:02

标签: asp.net-core

从Visual Studio 2017迁移到2019年我正在处理的应用程序后,不再对计算字段(FullName)进行排序。

计算字段将姓氏和名字字段连接起来以形成全名字段。 FullName字段用于对SelectList进行排序和填充。一切都按照VS 2017 CORE 2.2中的预期进行。出现错误消息,指示找不到“全名”字段。

我在数据库中看不到FullName列(DB表名称为Speaker)。如果我注释掉SelectList的查询的orderby(orderby s.FullName)部分,则名称不是按字母顺序排列的

在代码的代码部分中列出了用于创建表(模型)和OnPost方法中的查询的代码

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace PublicTalkSchedule.Models
{
    public class Speaker
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Display(Name = "Speaker #:")]
        public int Id { get; set; }

        [Required]
        [Display(Name = "Last Name:")]
        public string spkLastName { get; set; }
        [Required]
        [Display(Name = "First Name:")]
        public string spkFirstName { get; set; }

        [Display(Name = "Email:")]
        public string spkEmail { get; set; }

        [Display(Name = "E/MS:")]
        public string EldMs {get;set;}

        [NotMapped]
        [Display(Name = "")]
        public string FullName { get { return string.Concat(spkLastName + ", "
            + spkFirstName); } }

        [Display(Name = "Companyy:")]
        public string CompName { get; set; }

        [Required]
        public int CompId { get; set; }

        [ForeignKey("CompId")]
        public virtual Congregation Congregation { get; set; }

    }


// query speaker table to get a list of all speakers, insert them into 
//the SelectList (SpkID)
IQueryable<string> spknameQuery = from s in _db.Speaker       
                                  orderby s.FullName
                                  select s.FullName;

SpkId = new SelectList(await spknameQuery.ToListAsync());

对查询进行什么操作才能对计算所得的字段进行排序。数据库中应该有一个名为FullName的实际列吗?

1 个答案:

答案 0 :(得分:0)

我在以下示例中测试您的代码,名称按字母顺序排列:

1。型号:

public class Speaker
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Display(Name = "Speaker #:")]
    public int Id { get; set; }

    [Required]
    [Display(Name = "Last Name:")]
    public string spkLastName { get; set; }
    [Required]
    [Display(Name = "First Name:")]
    public string spkFirstName { get; set; }
    [NotMapped]
    [Display(Name = "")]
    public string FullName
    {
        get
        {
            return string.Concat(spkLastName + ", "
+ spkFirstName);
            }
        }
    [Display(Name = "Companyy:")]
    public string CompName { get; set; }

    [Required]
    public int CompId { get; set; }

    [ForeignKey("CompId")]
    public virtual Congregation Congregation { get; set; }
}
public class Congregation
{
    [Key]
    public int CompId { get; set; }
    public string CompName { get; set; }
}

2.Index.cshtml:

@page
@model IndexModel

<h1>Index</h1>
<form method="post">
    <input name="companyName" />
    <input type="submit" value="submit" />
</form>
@{
    if (Model.SpkId != null)
    {
        <select name="authorId" asp-items="Model.SpkId"></select>
    }
}

3.Index.cshtml.cs:

public class IndexModel : PageModel
{
    private readonly YourContext _db;

    public IndexModel(YourContext db)
    {
        _db = db;
    }
    public SelectList SpkId { get; set; }

    public void OnGetAsync()
    {
    }
    public async Task OnPostAsync(string companyName)
    {
        IQueryable<string> spknameQuery = from s in _db.Speaker
                                          orderby s.FullName
                                          select s.FullName;

        SpkId = new SelectList(await spknameQuery.ToListAsync());
    }
}

4。结果: enter image description here