简体   繁体   中英

Convert IQueryable to BindingList

Placing ObsevableCollection User from DataSource as Details on new form it creates all textBoxes, BindingSource and BindingNavigator. Which is excelent and fast.

Because I only need to update one user I removed BindingNavigator. But...

Can this be without a conversion of lists?

class UserDt : Forms {
    // Designer partial part
    this.userBindingSource.DataSource = typeof(WinFormswithEFSample.User);

    private void UserDt_Load
    {
        _context.Users.Load();

        // use this with BindNavigator to navigate ower all users
        //this.userBindingSource.DataSource = _context.Users.Local.ToBindingList();

        // this doesn't work
        //this.userBindingSource.DataSource = _context.Users.Where(p => p.Username == "admin").Local.ToBindingList();

        var query = _context.Users.Where(p => p.Username == "admin").ToList();
        var binding = new BindingList<User>(query);
        this.usersBindingSource.DataSource = binding;
    }
}

Can this be without a conversion of lists?

No.
The BindingList takes an IList as an argument.
IQueryable cannot be casted to an IList , so therefor you need to convert it as you have already done:

    var query = _context.Users.Where(p => p.Username == "admin")
                              .ToList(); //converts the IQueryable to List
    var binding = new BindingList<User>(query);

If you really need the BindingList and cannot settle for a simpler List

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