I am trying to select specific columns from a model but getting an error when I try to include select for the child entity. My model is -
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;}
}
I can use the following lambda expression to produce json data through Web API.
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());
The above code displays all the columns from the location table. I want to display specific columns from the location table and I tried the following code but getting error.
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.
Basically location is not allowing me to use select clause. Can anyone please help with this. Thanks in advance.
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 => new { l.Name, l.Latitude, ... })