【问题标题】:lambda expression in asp.net MVCasp.net MVC 中的 lambda 表达式
【发布时间】:2016-12-06 07:27:56
【问题描述】:

我正在尝试从模型中选择特定列,但在尝试为子实体包含选择时出现错误。我的模型是 -

public class Alert
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid AlertId { get; set; }

    [Display(Name = "Title"), MaxLength(160, ErrorMessage ="Title cannot be more than 200 characters long."), Required(ErrorMessage ="You must enter an alert title")]
   // [DataType(DataType.MultilineText)]
    public string Title { get; set; }


    [Display(Name = "Description"), MaxLength(8000, ErrorMessage = "Title cannot be more than 8000 characters long."), Required(ErrorMessage ="You must enter a description in {0} field")]
    [AllowHtml]
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

   [Display(Name = "Start Date"), Required(ErrorMessage ="You must enter the start date and time")]
   [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy hh:mm tt}", ApplyFormatInEditMode = false)]
  // [DataType(DataType.DateTime)]
    public DateTime StartDate { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy hh:mm tt}", ApplyFormatInEditMode = false)]
    [Display(Name = "End Date")]
    public DateTime? EndDate { get; set; }

    [Display(Name = "Affected Site/s"), MaxLength(200,ErrorMessage ="You cannot enter more than 200 characters.")]
    public string SitesAffected { get; set; }


    [MaxLength(10)]
    public string Published { get; set; }

    [Display(Name = "Website Link"), MaxLength(200,ErrorMessage ="This cannot be more than 200 characters.")]
    public string WebLink { get; set; }


    [Display(Name ="Alert Type")]
    public int AId { get; set; }

    public virtual ICollection<Location> Location { get; set; }
    public virtual ICollection<AlertFile> Files { get; set; }
    [JsonIgnore]
    public virtual AlertType alertType {get; set;}

}

我可以使用以下 lambda 表达式通过 Web API 生成 json 数据。

var alerts = db.Alerts.Where(a=>a.alertType.Slug== alert && a.EndDate>=DateTime.Now && a.Published=="Yes").Select(s=>new
        {s.Title, s.Description, s.alertType.Slug, s.StartDate, s.EndDate, s.Location
        });


        return Request.CreateResponse(HttpStatusCode.OK, alerts.ToList());

上面的代码显示了位置表中的所有列。我想显示位置表中的特定列,我尝试了以下代码但出现错误。

var alerts = db.Alerts.Where(a=>a.alertType.Slug== alert && a.EndDate>=DateTime.Now && a.Published=="Yes").Select(s=>new
        {s.Title, s.Description, s.alertType.Slug, s.StartDate, s.EndDate, s.Location.Select(l => new Location { l.Name, l.Latitude, l.Longitude, l.ParkId, l.Contact })
        });

错误:无效的匿名类型成员声明符。必须使用成员分配、简单名称或成员访问来声明匿名类型成员。

基本上位置不允许我使用选择子句。谁能帮忙解决这个问题。提前致谢。

【问题讨论】:

  • 您遇到了什么错误?
  • Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
  • locations = s.Location.Select(l =&gt; new { l.Name, l.Latitude, ... })
  • 哇,成功了。谢谢斯蒂芬。

标签: c# asp.net asp.net-mvc lambda


【解决方案1】:

用这一行:

s.Location.Select(l => new Location { l.Name, l.Latitude, l.Longitude, l.ParkId, l.Contact }

您不会将值分配给任何属性,如果您想将所选列表分配给属性,就像@Stephen Muecke 所说的,您应该选择它:

Location = s.Location.Select(l => new Location { l.Name, l.Latitude, l.Longitude, l.ParkId, l.Contact }

实际上你的完整查询应该是这样的:

var alerts = db.Alerts.Where(a=>a.alertType.Slug== alert && a.EndDate>=DateTime.Now && a.Published=="Yes").Select(s=>new
        { 
            Title = s.Title, 
            Description = s.Description, 
            AlertType = s.alertType.Slug, 
            StartDate = s.StartDate, 
            EndDate = s.EndDate, 
            Location = s.Location.Select(l => new Location { l.Name, l.Latitude, l.Longitude, l.ParkId, l.Contact })
        });

但是 C# 编译器知道如何命名简单的属性。

【讨论】:

    【解决方案2】:

    您需要将您的属性命名为匿名类型:

    var alerts = db.Alerts.Where(a=>a.alertType.Slug== alert && a.EndDate>=DateTime.Now && a.Published=="Yes").Select(s=>new
        {
          s.Title, s.Description, s.alertType.Slug, s.StartDate, s.EndDate,
          location = s.Location.Select(l => new Location { l.Name, l.Latitude, l.Longitude, l.ParkId, l.Contact })
        });
    

    如果您只是选择一个简单的属性,则会自动使用该属性的名称,但对于其他选择,您需要自己命名。

    【讨论】:

      【解决方案3】:

      如果您遇到同样的问题。正如斯蒂芬建议的那样,我已将 lambda 表达式更改为以下内容。现在可以正常使用了。

      var alerts = db.Alerts.Where(a=>a.alertType.Slug== alert && a.EndDate>=DateTime.Now && a.Published=="Yes").Select(s=>new
              {s.Title, s.Description, s.alertType.Slug, s.StartDate, s.EndDate, locations= s.Location.Select(l => new { l.Name, l.Latitude, l.Longitude, l.ParkId, l.Contact })
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-18
        • 1970-01-01
        • 2010-11-11
        • 2017-01-09
        • 2010-12-10
        • 2012-07-26
        • 2020-04-25
        • 1970-01-01
        相关资源
        最近更新 更多