如何查找EntityCollection中的实体?

发布于 2024-08-09 16:22:42 字数 284 浏览 2 评论 0原文

我使用 EntityCollection 作为组合框的数据源,作为带有 ria 服务的 silverlight 应用程序的下拉列表。 如果我知道集合中选定的项目,假设其 ID=123,那么我可以使用此 ID 来查找 EntityCollection 中选定的项目。如何编写用于此目的的通用函数?

说这样的话:

public Entity<T> GetEntity(EntityCollection<T> collection, string ID)
{


}

I use EntityCollection as datasource for combox as dropdown for a silverlight app with ria services.
If I know the item selected in the collection, say its ID=123, then I can use this ID to find out the selected in EntityCollection. How to write the general function for this purpose?

say something like:

public Entity<T> GetEntity(EntityCollection<T> collection, string ID)
{


}

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

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

发布评论

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

评论(1

不羁少年 2024-08-16 16:22:42

我不确定您是否可以在不使用反射的情况下将其设为通用函数。如果您知道要查找的类的类型,那么您可以使用 Linq 来查找对象:

var x = collection.Select(o => o as MyObject).Where(o => o.ID == ID).FirstOrDefault();

在这种情况下,您需要知道您正在使用的对象上有一个名为 ID 的属性。如果您不知道这一点,那么您将不得不使用反射来获取 ID 属性并比较该值(AFIAK)。

由于它实际上只是一行代码,因此可能不值得付出努力。

I'm not sure you can make this a general purpose function without using reflection. If you know the type of class you're looking for then you can just use Linq to find the object:

var x = collection.Select(o => o as MyObject).Where(o => o.ID == ID).FirstOrDefault();

In that case you would need to know that there is a property called ID on the object you're using. If you don't know this then you would have to use reflection to get the ID property and compare the value (AFIAK).

Since it really is only one line of code it probably isn't worth the effort.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文