locked
Possible to Get Object Set By Name RRS feed

  • Question

  • Possible to Get Object Set By Name ? in order to transform below code 1 to code 2 ?

    Code 1:

            public static object GetEntity(string EntityCode, object DB, Guid xKey)
            {
                switch (EntityCode)
                {
                    case "Database": // Entity Name : Database
                        return (from f in ((EntityModel)DB).Databases where f.DatabaseRef == xKey select f).FirstOrDefault();
                    case "Project":  // Entity Name : Project
                        return (from f in ((EntityModel)DB).Projects where f.ProjectRef == xKey select f).FirstOrDefault();
                    default:
                        return null;
                }
            }

    Code 2 :

            public static object GetEntity(string EntityCode, object DB, Guid xKey)
            {

                 object  aaaaa = GetObjectSetByName(EntityCode); // Something Like This

                 // And how to solve "f.DatabaseRef" part ?

                 return (from f in ((EntityModel)DB).aaaaa where f.DatabaseRef == xKey select f).FirstOrDefault();
            }

    Wednesday, March 27, 2013 2:25 PM

Answers

  • Hi  Feuner,

    Welcome to the MSDN forum.

    As far as I know, you are required to know the Entity type of the requested ObjectSet<TEntity>. Then you could invoke the CreateObjectSet<TEntity>() method in ObjectContext:

    var type = Type.GetType(myTypeName);

    var method = _ctx.GetType().GetMethod("CreateObjectSet", Type.EmptyTypes);

    var generic = method.MakeGenericMethod(type);

    dynamic objectSet = generic.Invoke(_ctx, null);

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Thursday, April 4, 2013 9:16 AM
    Thursday, March 28, 2013 8:10 AM