我有以下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
的值DeepLink
和a
。 a
是以下模型的实例:
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给我以下结果
错误文字:
Cannot assign 'lambda expression' to anonymous type property