Domanda

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;
    }
}
È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top