2016-12-06 65 views
1

我想從模型中選擇特定的列,但在嘗試爲子實體包含select時出錯。我的模型是 -在asp.net中的lambda表達式MVC

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子句。任何人都可以請幫忙。提前致謝。

+0

你得到的錯誤是什麼? –

+0

'匿名類型成員聲明符無效。匿名類型成員必須使用成員賦值,簡單名稱或成員訪問來聲明。' – Tajuddin

+3

'locations = s.Location.Select(l => new {l.Name,l.Latitude,...})' –

回答

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

如果你只選擇一個簡單的屬性的屬性的名稱會自動使用,但對於其他選擇,你需要將其命名爲你自己。

+0

完全正確,謝謝@ChrFin – Tajuddin

0

如果你有人有同樣的問題。正如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 }) 
     }); 
3

這一行:

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#編譯器知道如何命名簡單屬性。