Linq 和 ObservableCollection

发布于 2024-07-25 02:44:52 字数 1450 浏览 6 评论 0原文

我的 WPF 应用程序中的 Linq 和 ObservableCollections 存在问题。

问题的背景:

我创建了一个非常简单的 SQL 数据库,其中包含两个表:User 和 BankAccounts。 User 表与 BankAccounts 表具有一对多关系。 接下来,我创建了 Linq-to-SQL 数据类,效果很好 ==> 两个表之间的关联也被检测到。

接下来,我创建了一个函数来检索所有工作正常的用户:

DataClassesDataContext dc = new DataClassesDataContext

var query = from u in dc.Users
            select u;

现在假设我想向每个用户添加一个新的 BankAccount(不太可能,但仍然如此)。 我可以添加以下代码

for each(User u in query)
{
   u.BankAccounts.Add(New BankAccount());
}

以上工作正常。 由于数据库和 Linq DataClasses 中的关联,BankAccounts 属性自动成为 User 类的一部分。

但是,在我的应用程序中,我首先将查询结果添加到 ObservableCollection 中。 因此我可以使用各种数据绑定和更改通知。 这是通过以下代码完成的;

ObservableCollection<User> oUsers = new ObservableCollection<User>(query);

问题:在 ObservableCollection 中,我无法对用户 BankAccounts 属性执行任何操作,因为它现在是 EntitySet<> 类型。 所以我不能再做下面的声明了。

for each(User u in oUsers)
{
   u.BankAccounts.Add(New BankAccount());
}

不知何故,当将查询结果添加到可观察集合时,将无法再访问 user.BankAccounts 属性。 但是,可以将 BankAccounts 属性绑定到任何控件(例如列表框),并且它包含正确的数据。

有人现在如何创建一个 observableCollction (或类似的集合),我可以从中访问这些“关联”属性? 我真的很期待一个解决方案。

提前致谢! 谨致问候,

巴斯·兹韦里斯 E: [电子邮件受保护]

I have a problem with Linq and ObservableCollections in my WPF application.

Context of the problem:

I've created a very simple SQL database with two tables: User and BankAccounts.
The User Table has an one-to-many relationship with the BankAccounts Table. Next I've created Linq-to-SQL dataclasses, which worked fine ==> the assosiation between the two tables was detected as well.

Next I've created a function to retreive all Users which works fine:

DataClassesDataContext dc = new DataClassesDataContext

var query = from u in dc.Users
            select u;

Now suppose I want to add a new BankAccount to each user (not very likely but still).
I could add the following code

for each(User u in query)
{
   u.BankAccounts.Add(New BankAccount());
}

The above works all fine. The BankAccounts property is automaticly part of the User class, due to the assosiation in the database and Linq DataClasses.

However, in my application I first add the query results to an ObservableCollection. Hereby I could use all sorts off databinding and changenotification. This is accomplished by the following code;

ObservableCollection<User> oUsers = new ObservableCollection<User>(query);

Problem: Within the ObservableCollection I can't do anyting with the users BankAccounts property because it is now of type EntitySet<>. So I can't do the following statement anymore.

for each(User u in oUsers)
{
   u.BankAccounts.Add(New BankAccount());
}

Somehow, when queryresults are added to an observablecollection It is not possible to acces the user.BankAccounts properties anymore. However, it is possible to bind the BankAccounts Property to any control, like a listbox, and it contains the correct data.

Does someone now how I can create an observableCollction (or similar collection) from wich I can access these "assosiated" properties? I'm realy looking forward for to a solution.

Thanks in advance!
Best regards,

Bas Zweeris
E: [email protected]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2024-08-01 02:44:52

跟踪将实现 IQueryable 的原始查询,您可以针对该查询运行您需要的任何进一步查询。

ObservableCollection 应该只是让 WPF 有一些可以绑定的东西 - 如果您想添加一个新的集合项,但在用户有机会编辑它之前不将其推送到数据库,那么它非常有用。

例如。

// Create a new blank client type
var ct = new ClientType()
{
    IsArchived = false,
    Description = "<new client type>",
    Code = "CT000",
    CanLoginOnline = true
};

// Tell the data source to keep track of this object
db.ClientTypes.InsertOnSubmit(ct);

// Also add the object to the observable collection so that it can immediately be shown in the UI and editted without hitting the db
clienttypes.Add(ct);

Keep track of the original query which will implement IQueryable, you can run any further queries you need against that.

The ObservableCollection should just be for WPF to have something to bind to - its very useful if you want to add a new collection item but not have it pushed to the database before the user has had chance to edit it.

eg.

// Create a new blank client type
var ct = new ClientType()
{
    IsArchived = false,
    Description = "<new client type>",
    Code = "CT000",
    CanLoginOnline = true
};

// Tell the data source to keep track of this object
db.ClientTypes.InsertOnSubmit(ct);

// Also add the object to the observable collection so that it can immediately be shown in the UI and editted without hitting the db
clienttypes.Add(ct);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文