无法在此范围内声明名称为local或parameter的名称,因为该名称在封闭的本地范围内用于定义本地或参数

时间:2019-06-13 16:37:12

标签: c# linq

我有以下linq代码

var advertisements = _applicationContext.Advertisings.ToList();

var stepsData = advertisements.Select(a => new[]
{
    new
    {
        number = a.Number,
        text = a.Text,
        image = a.Image,
        type = a.Type,
        data = (a) =>
        {
            if (string.IsNullOrEmpty(a.Link) || string.IsNullOrEmpty(a.Deeplink))
            {
                return null;
            }

            return new
            {
                link = a.Link,
                deeplink = a.Deeplink
            };
        }
    }
});

请参见data属性。此属性的值取决于项Link的值DeepLinkaa是以下模型的实例:

public class Advertising
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public int Number { get; set; }

    public string Text { get; set; }

    public string Image { get; set; }

    public string Type { get; set; }

    public bool Show { get; set; }

    public string Link { get; set; }

    public string Deeplink { get; set; }

    public DateTime CreatedAt { get; set; }

    public DateTime UpdatedAt { get; set; }

    public Advertising()
    {
        CreatedAt = DateTime.UtcNow;
        UpdatedAt = DateTime.UtcNow;
    }
}

一切都很好,但是我看到了来自IDE的通知 A local or parameter named 'a' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

在这种情况下如何使用lambda?我需要传递给lambda变量a。没有参数,它向我显示以下错误 Cannot assign 'lambda expression' to anonymous type property

更新

没有参数的Lambda给我以下结果

enter image description here

错误文字: Cannot assign 'lambda expression' to anonymous type property

0 个答案:

没有答案