简体   繁体   English

asp.net MVC中的lambda表达式

[英]lambda expression in asp.net MVC

I am trying to select specific columns from a model but getting an error when I try to include select for the child entity. 我试图从模型中选择特定的列,但是在尝试为子实体包括select时却出现错误。 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. 我可以使用以下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());

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. 基本上位置不允许我使用select子句。 Can anyone please help with this. 任何人都可以帮忙。 Thanks in advance. 提前致谢。

With this line: 用这行:

s.Location.Select(l => new Location { l.Name, l.Latitude, l.Longitude, l.ParkId, l.Contact }

You don't assign your values to any property, you just select it if you want to assign your selected list to property like @Stephen Muecke said you should write: 您不会将值分配给任何属性,如果要将所选列表分配给属性,例如@Stephen Muecke说您应该编写,则只需选择它即可:

Location = s.Location.Select(l => new Location { l.Name, l.Latitude, l.Longitude, l.ParkId, l.Contact }

Actually your full query should look like this: 实际上,您的完整查询应如下所示:

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

But C# compiler know how to name simple properties. 但是C#编译器知道如何命名简单属性。

You need to name your property in the anonymous type: 您需要使用匿名类型来命名您的媒体资源:

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

If you just select a simple property the name of the property is used automatically, but for other selects you need to name it on your own. 如果仅选择一个简单属性,则将自动使用该属性的名称,但对于其他选择,则需要自己命名。

In case if you someone has the same issue. 如果有人遇到相同的问题。 I have changed the lambda expression to the following as Stephen suggested. 正如Stephen所建议的,我将lambda表达式更改为以下表达式。 It works fine now. 现在工作正常。

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.

相关问题 从ASP.NET MVC Lambda Expression获取价值 - Get value from ASP.NET MVC Lambda Expression lambda 表达式连接多个表 ASP.NET MVC 5 - lambda expression join multiple tables ASP.NET MVC 5 ASP.NET MVC:如何将 linq lambda 表达式与 ViewModel 一起使用来获取数据? - ASP.NET MVC: How to use linq lambda expression with a ViewModel to get data? MVC 5 ASP.Net Razor无法将lambda表达式转换为类型“ MvcHtmlString”,因为它不是委托类型 - MVC 5 ASP.Net Razor Cannot convert lambda expression to type 'MvcHtmlString' because it is not a delegate type 实体框架asp.net mvc5-无法将lambda表达式转换为类型“字符串”,因为它不是委托类型 - Entity Framework asp.net mvc5- Cannot convert lambda expression to type 'string' because it is not a delegate type ASP.NET MVC:不能将lambda表达式用作动态分派操作的参数 - ASP.NET MVC: Cannot use a lambda expression as an argument to a dynamically dispatched operation ASP.NET MVC 错误无法将 lambda 表达式转换为类型 'string' 因为它不是委托类型 - ASP.NET MVC error cannot convert lambda expression to type 'string' because it is not a delegate type 如何将指定属性的lambda表达式转换为与asp.net mvc兼容的“名称”字符串来表示相同内容? - How can I turn a lambda expression specifying a property into an asp.net mvc compatible 'name' string representing the same? 如何在C#中使用带有asp.net DataList的lambda表达式 - How to use lambda expression with asp.net DataList in C# LINQ ASP.NET中嵌套lambda表达式中的参数引用错误 - Error in referencing parameters in nested lambda expression in LINQ ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM