none
Code First - Return anonymous object with dynamic - ExpandoObject RRS feed

Answers

  • Hi Leandro,

    For this requirement, we could use IEnumerable<dynamic> or dynamic as the returned type.

    For example:

    public static IEnumerable<dynamic> GetDynamic() { MessageInfoEntities1 db = new MessageInfoEntities1(); return (from d in db.UserInfoes select new{id=d.Id,name=d.UserName}).ToList();

    //or

    return (from d in db.UserInfoes
                        select new{id=d.Id,name=d.UserName}); } public static dynamic GetDynamic2() { MessageInfoEntities1 db = new MessageInfoEntities1(); return (from d in db.UserInfoes select new { id = d.Id, name = d.UserName }).First(); }

    Best Regards

    Starain Chen


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, October 1, 2014 9:01 AM
    Moderator

All replies

  • Hi Leandro,

    For this requirement, we could use IEnumerable<dynamic> or dynamic as the returned type.

    For example:

    public static IEnumerable<dynamic> GetDynamic() { MessageInfoEntities1 db = new MessageInfoEntities1(); return (from d in db.UserInfoes select new{id=d.Id,name=d.UserName}).ToList();

    //or

    return (from d in db.UserInfoes
                        select new{id=d.Id,name=d.UserName}); } public static dynamic GetDynamic2() { MessageInfoEntities1 db = new MessageInfoEntities1(); return (from d in db.UserInfoes select new { id = d.Id, name = d.UserName }).First(); }

    Best Regards

    Starain Chen


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, October 1, 2014 9:01 AM
    Moderator
  • Hi

    many thanks, works perfect

    I create the method

            public IEnumerable<dynamic> TestMethod()
            {
                using (XXContext context = new XXContext())
                {
                    var result = context.Customers.Select(x=> new
                    {
                        Id = x.CustomerID,
                        Description = x.LastName + ", " + x.FirstName
                    });

                    return result.ToList();
                }
            }

    and use el ToList() to force the execution of linq query

    the original entity has many properties and I only need two, but I don't want define the class

    I wanted to return a anonymous, and I made it now, thanks

    regards


    Leandro Tuttini

    Blog
    MVP Profile
    Buenos Aires
    Argentina

    Wednesday, October 1, 2014 8:17 PM