gpt4 book ai didi

c# - asp.net MVC 中的 lambda 表达式

转载 作者:太空宇宙 更新时间:2023-11-03 19:49:49 24 4
gpt4 key购买 nike

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

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 })
});

Error: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

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

最佳答案

用这一行:

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# 编译器知道如何命名简单的属性。

关于c# - asp.net MVC 中的 lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40989982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com