views:

170

answers:

1

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)
{


}
+1  A: 

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.

Bryant
Thank you. Select is not available for EntityCollection<T>. When I try Collection. in VS, intelligence do not show Select. Even I apply it on a EntityCollection<Employee>.
KentZhou
After adding using System.Linq, it shows up. Thanks.
KentZhou