[英]lambda expression in asp.net MVC
我试图从模型中选择特定的列,但是在尝试为子实体包括select时却出现错误。 我的模特是-
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 })
});
错误:无效的匿名类型成员声明符。 必须使用成员分配,简单名称或成员访问来声明匿名类型成员。
基本上位置不允许我使用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#编译器知道如何命名简单属性。
您需要使用匿名类型来命名您的媒体资源:
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 })
});
如果仅选择一个简单属性,则将自动使用该属性的名称,但对于其他选择,则需要自己命名。
如果有人遇到相同的问题。 正如Stephen所建议的,我将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 })
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.