如何查找EntityCollection中的实体?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您是否可以在不使用反射的情况下将其设为通用函数。如果您知道要查找的类的类型,那么您可以使用 Linq 来查找对象:
在这种情况下,您需要知道您正在使用的对象上有一个名为 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:
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.