0

I have the following linq code

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

See on the data property. A value of this property depends on values Link and DeepLink of item a. The a is an instance of the following model:

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

All is good, but I see a notification from 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

How to use lambda in this case? I need to pass into the lambda variable a. Without params, it shows me the following error Cannot assign 'lambda expression' to anonymous type property

Update

Lambda without params gives me the following result

enter image description here

Error text: Cannot assign 'lambda expression' to anonymous type property

Aleksej_Shherbak
  • 2,757
  • 5
  • 34
  • 71
  • 1
    **1.** You're re-using `a` (which is already defined in this scope) for `data = (a) => { ... }`. **2.** How do you expect the compiler to infer the type of the `data` property of your anonymous type? – haim770 Jun 13 '19 at 16:40
  • Just use a different name.... `data => b => {...}` – Zohar Peled Jun 13 '19 at 16:40
  • After changing name I see `Cannot assign 'lambda expression' to anonymous type property` – Aleksej_Shherbak Jun 13 '19 at 16:43
  • `a` is the argument of the enclosing anonymous lambda associated with the Select statement, then you use it again as the argument of the lambda associated with the data property. if you change `data=(a)=>` to `data =()=>` I believe you wouldn't get that error. – jimboweb Jun 13 '19 at 16:43
  • 1
    See this answer to explain why you're getting the other error: https://stackoverflow.com/questions/8128625/why-cant-an-anonymous-class-have-a-lambda-property-but-it-can-have-a-func-pr – jimboweb Jun 13 '19 at 16:46
  • 2
    @Aleksej_Shherbak, Try to rewrite your assignment as `data = string.IsNullOrEmpty(a.Link) || string.IsNullOrEmpty(a.Deeplink) ? null : new { ... }` – haim770 Jun 13 '19 at 16:48
  • @haim770 nice hack))) I will use it to avoid this problem just now)) Thank you)) – Aleksej_Shherbak Jun 13 '19 at 16:52
  • 1
    Slighty off-topic, the `ToList()` call in the first line brings the whole DB into memory before doing anything is almost never a good idea and very likely to give bad performance. – Alejandro Jun 13 '19 at 16:55
  • 1
    If @haim770's answer works for you, that means you didn't need a lambda in the first place. You want `data` to reference either another anonymous type or null. As you had originally written it, you were setting `data` to equal a *function* that returned an anonymous type or null. – Scott Hannen Jun 13 '19 at 17:00
  • @Alejandro could you give me some proofs? Maybe some article. Where I can read about it? – Aleksej_Shherbak Jun 14 '19 at 07:17

0 Answers0