Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting List of Objects by a type given by a string

I get objects by

IEnumerable<ObjectStateEntry> om = context.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified);        

How can I get a List of the objects with a type given by a string?

Type typ = Type.GetType("mytype");
var om2 = om.Select(s => s.Entity).OfType<typ>(); // does not work
like image 204
daniel Avatar asked Nov 07 '12 11:11

daniel


People also ask

How do I convert a list of strings to a list of objects?

Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList); Any Collection can be passed as an argument to the constructor as long as its type extends the type of the ArrayList , as String extends Object .

Can we have list of objects?

Yes, it is absolutely fine to have an object contains list of object.

Can we convert list to object?

A list can be converted to a set object using Set constructor. The resultant set will eliminate any duplicate entry present in the list and will contains only the unique values.


1 Answers

What you are trying to do cannot be done statically: var corresponds to the static type of the expression, while the type of your expression on the right is clearly non-static (it's IEnumerable<T>, where T is not known before the runtime).

This, however, is legal:

var om2 = om.Select(s => s.Entity).Where(v => typ.IsInstanceOfType(v));

This would produce an IEnumerable<ObjectStateEntry>.

like image 147
Sergey Kalinichenko Avatar answered Sep 25 '22 16:09

Sergey Kalinichenko