简体   繁体   中英

Method for getting a list async? 'List<ListDefinition>' does not contain a definition for 'ToListAsync'

I am new to asp.net core programming, and trying to make a simple list application, managing a number of lists defined by ListDefinition, and each list containing several ListItems. I am trying to use async methods, which works fine when getting the ListDefinition ( var currentList = await _context.ListDefinitions.Where(m => m.Id == id).FirstOrDefaultAsync(); )

However when trying to get the Listitems, I find no suitable method of getting it async ( var currentListItems = await currentList.ListItems.Distinct().ToListAsync(); does not exist)

Models

public class ListDefinition
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int Id { get; set; }
    public Guid CreatedByUserGuid { get; set; }
    public string Name { get; set; } 
    public virtual ICollection<ListItem> ListItems { get; set; }
}

public class ListItem
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int Id { get; set;            
    public string Description { get; set; }
    public bool Done { get; set; }
    public bool Important { get; set; }
}

Controller

public class CurrentListController : Controller
{
    private readonly ApplicationDbContext _context;

    public CurrentListController(ApplicationDbContext context)
    {
        _context = context;
    }

    // GET: CurrentList/5
    public async Task<IActionResult> Index(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var currentList = await _context.ListDefinitions.Where(m => m.Id == id).FirstOrDefaultAsync();
        var currentListItems = await currentList.ListItems.Distinct().ToListAsync();
        if (currentListItems == null)
        {
            return NotFound();
        }

        return View(currentListItems);
    }
(...)

The error message:

'List<ListDefinition>' does not contain a definition for 'ToListAsync' and no extension method 'ToListAsync' accepting a first argument of type 'List<ListDefinition>'

You have two options:

Don't leverage lazy loading. Grab all the data in one hit:

var currentList = await _context.ListDefinitions.Where(m => m.Id == id)
                                .Include(ld => ld.ListItems).FirstOrDefaultAsync();

This means the data is already loaded, so there's no benefit in using async in the next call:

var currentListItems = await currentList.ListItems.Distinct().ToList();

However, since you're using a Distinct (what for? Perhaps there shouldn't be duplicates in the database?), you can potentially be loading too much data.

Because you don't actually use current list, you can simply load the items:

var currentListItems = _context.ListItems
                               .Where(li => li.ListDefinitionId == id)
                               .Distinct().ToListAsync();

This does of course mean you'll need to define the foreign key on ListItem to match up with public virtual ICollection<ListItem> ListItems { get; set; } public virtual ICollection<ListItem> ListItems { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM