簡體   English   中英

asp.net MVC中的lambda表達式

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM