locked
variable 'ct' of type 'System.Threading.CancellationToken' referenced from scope '', but it is not defined RRS feed

  • Question

  • User1255309776 posted

    Hi,

    I have used the following LINQ code for my viewcomponent menu, and faced exception written in Title which I could'n understand why. I even didn't declare variable 'ct' anytwhere. Did someone face such issue? 

    public async Task<IViewComponentResult> InvokeAsync()
            {
                    var menu = await _samirDbContext.Categories.Include(x => x.Subcategories).ThenInclude(y => y.NestedCategories).
                                                              Select(x => new MenusModel()
                                                              {
                                                                  Category = x,
                                                                  Id = x.Id,
                                                                  Subcategories = x.Subcategories,
                                                                  Subcategory = x.Subcategories.FirstOrDefault(),
                                                                  NestedCategories = x.Subcategories.FirstOrDefault().NestedCategories.Select(z => new NestedCategory()
                                                                  {
                                                                      Id = z.Id,
                                                                      Name = z.Name
                                                                  })
    
                                                              }).ToListAsync();
                    
                    return View(menu);
                                                                                           
            }

    Thanks in advance

    Saturday, August 29, 2020 1:42 PM

All replies

  • User475983607 posted

    The error means the variable you are trying to use is out of scope.  I assume the confusion has to do the the "x" parameter since it is being used twice.  The "x" represents two completely different objects.

    It seems to me, the LINQ query is overly complex for a simple join.   The community can provide better assistance if you share the object model and table design.  

    Saturday, August 29, 2020 2:30 PM
  • User-474980206 posted

    Most likely the error is unrelated to code you are showing, but from another async call.

    Saturday, August 29, 2020 2:40 PM
  • User1255309776 posted

    But when I hover over the x, everywhere it presents x as "(parametr) Category x" which means Category object.

    When it comes to models, you can see them from this link of my previous issue:

    https://forums.asp.net/t/2170253.aspx?Unexpected+INSERT+statement+conflict+with+the+FOREIGN+KEY+error+after+create+action

    Also, if you know better LINQ, can you write it please? You can refer to below link to see my Default view and other details. 

    https://forums.asp.net/t/2170317.aspx?Problem+with+writing+LINQ+for+component+view+Dropdown+menu+

    Thanks

    Saturday, August 29, 2020 4:26 PM
  • User1255309776 posted

    If the error comes from another async, can you show me how to easily search for "ct" variable which I didn't create?)  I even don't know where 

    System.Threading.CancellationToken exists.

    Saturday, August 29, 2020 4:28 PM
  • User1255309776 posted

    What I've also noticed, it works when excludeing 3rd level (Nestedcategory) definition from the LINQ, in other words, with this:

      public async Task<IViewComponentResult> InvokeAsync()
            {
                    var menu = await _samirDbContext.Categories.Include(x => x.Subcategories).ThenInclude(y => y.NestedCategories).
                                                              Select(x => new MenusModel()
                                                              {
                                                                  Category = x,
                                                                  Id = x.Id,
                                                                  Subcategories = x.Subcategories.Select(y => new Subcategory()
                                                                  {
                                                                      Id = y.Id,
                                                                      Name = y.Name
                                                                  })                                                     
    
                                                              }).ToListAsync();
                 
                    return View(menu);
                                                                                           
            }

    Then it means problem must be searched within this part of my full InvokeAsync() method code:

      Subcategory = x.Subcategories.FirstOrDefault(),
                                                                  NestedCategories = x.Subcategories.FirstOrDefault().NestedCategories.Select(z => new NestedCategory()
                                                                  {
                                                                      Id = z.Id,
                                                                      Name = z.Name
                                                                  })
    

    What might cause this error?

    Saturday, August 29, 2020 5:07 PM
  • User1255309776 posted

    I also tried to correct it this way, but again face same exception :

    variable 'ct' of type 'System.Threading.CancellationToken' referenced from scope '', but it's not defined.

    What is incorrect in this code? And what the hell is "ct"?)

       public async Task<IViewComponentResult> InvokeAsync()
            {
                var menu = await _samirDbContext.Categories.Include(x => x.Subcategories).ThenInclude(y => y.NestedCategories).
                                                          Select(x => new MenusModel()
                                                          {
                                                              Category = x,
                                                              Id = x.Id,
                                                              Subcategories = x.Subcategories,
                                                              NestedCategories = x.Subcategories.Select(y => new Subcategory(){
                                                                  Id = y.Id,
                                                                  Name = y.Name
                                                              }).SelectMany(y => y.NestedCategories).Select(z => new NestedCategory()
                                                              {
                                                                  Id = z.Id,
                                                                  Name = z.Name
                                                              })
                                                          }).ToListAsync();
                 
                    return View(menu);
                                                                                           
            }

    Saturday, August 29, 2020 8:52 PM
  • User711641945 posted

    Hi FaridGN,

    Could you share your whole models?

    Here is a working demo:

    Model:

    public class Category
    {
        public int Id { get; set; }
        public string CName { get; set; }
        public List<Subcategory> Subcategories { get; set; }
    }
    public class Subcategory
    {
        public int Id { get; set; }
        public string SName { get; set; }
        public List<NestedCategory> NestedCategories { get; set; }
    }
    public class NestedCategory
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    public class MenusModel
    {
        public int Id { get; set; }
        public Category Category { get; set; }
        public Subcategory Subcategory { get; set; }
        public List<Subcategory> Subcategories { get; set; }
        public List<NestedCategory> NestedCategories { get; set; }
    }

    Controller:

    var menu = await _context.Categories.Include(x => x.Subcategories).ThenInclude(y => y.NestedCategories).
                                                    Select(x => new MenusModel()
                                                    {
                                                        Category = x,
                                                        Id = x.Id,
                                                        Subcategories = x.Subcategories,
                                                        Subcategory = x.Subcategories[0],
                                                        NestedCategories = x.Subcategories.FirstOrDefault().NestedCategories.Select(z => new NestedCategory()
                                                        {
                                                            Id = z.Id,
                                                            Name = z.Name
                                                        }).ToList()
    
                                                    }).ToListAsync();

    Best Regards,

    Rena

    Monday, August 31, 2020 9:41 AM