3

ObsevableCollection UserDataSource 作为 Details 放置在新表单上,它会创建所有文本框、BindingSource 和 BindingNavigator。这是优秀和快速的。

因为我只需要更新一个用户,所以我删除了 BindingNavigator。但...

这可以不转换列表吗?

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

1 回答 1

5

这可以不转换列表吗?


。 TheBindingList接受 anIList作为参数。
IQueryable不能转换为IList,因此您需要像已经完成的那样转换它:

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

如果你真的需要并且BindingList不能满足于更简单的List

于 2013-11-01T13:16:00.203 回答