return list with anonymous type in entity framework

48,540

Solution 1

The class Tuple<> is made for situations like this. Creating a custom class as already suggested is clearer, but Tupple gets the job done too.

e.g.

.Select(row => new Tuple<int,string>(row.IdMember,row.Profile_Information.UserName))

to access the member properties at the other side of the wire, you'll need to use:

var id=t.Item1
var name=t.Item2

Solution 2

Because you're returning objects of an anonymous type, you cannot declare that in your return type for the method. Your attempted use of the generic <T> won't work for this. There is no type-safe way to declare a method like that. If you declare your return type as IList then that should work, but you still won't have type safety.

You're really just better off declaring the type.

Update:

Declaring a simple return type isn't so bad. You could write a class like this:

public class MemberItem
{
    public string IdMember { get; set; }
    public string UserName { get; set; }
}

And then write your method like this:

public static List<MemberItem> GetMembersItems(string ProjectGuid)
{
    using (PMEntities context = new PMEntities("name=PMEntities"))
    {
        var items = context.Knowledge_Project_Members.Include("Knowledge_Project").Include("Profile_Information")
                    .Where(p => p.Knowledge_Project.Guid == ProjectGuid)
                    .Select(row => new MemberItem { IdMember = row.IdMember, UserName = row.Profile_Information.UserName });

        return items.ToList();
    }
}

Solution 3

Scope of anonymous types are limited to the method in which they are defined. In your case, you better declare a separate class with the relevant properties and return a collection of the instances of that class. For e.g.

public class UserDetail
{
   public int Id{get;set;}
   public string UserName {get;set;}
}

public static List<UserDetail> GetMembersItems(string ProjectGuid)
    {
        using (PMEntities context = new PMEntities("name=PMEntities"))
        {
            var items = context.Knowledge_Project_Members.Include("Knowledge_Project").Include("Profile_Information")
                        .Where(p => p.Knowledge_Project.Guid == ProjectGuid)
                        .Select(row => new UserDetail{ IdMember = row.IdMember, UserName = row.Profile_Information.UserName });

            return items.ToList();
        }
    }

Solution 4

You can return anonymous types by casting them to Object, but this is rarely useful. However if you are returning it from say an WebApi controller as a quick and (not so) dirty DTO then I think it is perfectly fine. This only works with JSON though. XML will complain about the schema or something, but nowadays everybody use JSON anyway :)

public static List<Object> GetMembersItems(string ProjectGuid)
{
    using (PMEntities context = new PMEntities("name=PMEntities"))
    {
        var items = context.Knowledge_Project_Members.Include("Knowledge_Project").Include("Profile_Information")
                    .Where(p => p.Knowledge_Project.Guid == ProjectGuid)
                    .Select(row => new { IdMember = row.IdMember, UserName = row.Profile_Information.UserName });

        return items
               .ToList() // this is only needed to make EF happy otherwise it complains about the cast
               .Cast<Object>()
               .ToList();
    }
}

Solution 5

Just use and ArrayList instead

    public static ArrayList GetMembersItems(string ProjectGuid)
    {
        ArrayList items = new ArrayList(); 

              items.AddRange(yourVariable 
                        .Where(p => p.Knowledge_Project.Guid == ProjectGuid)
                        .ToList());
            return items;
    }
Share:
48,540
Pablogrind
Author by

Pablogrind

Updated on July 09, 2022

Comments

  • Pablogrind
    Pablogrind almost 2 years

    How i can return list with anonymous type, because with this code i get

    "The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)"

    i need only return IdMember and UserName, thanks

        public static List<T> GetMembersItems(string ProjectGuid)
        {
            using (PMEntities context = new PMEntities("name=PMEntities"))
            {
                var items = context.Knowledge_Project_Members.Include("Knowledge_Project").Include("Profile_Information")
                            .Where(p => p.Knowledge_Project.Guid == ProjectGuid)
                            .Select(row => new { IdMember = row.IdMember, UserName = row.Profile_Information.UserName });
    
                return items.ToList();
            }
        }
    
  • Pablogrind
    Pablogrind almost 13 years
    thanks for reply, which is the best way to return a list with this select?
  • Rabbi
    Rabbi about 11 years
    Tuples dont work in entity framework because you need to create them with their intializer function and entity framework only allows you to use types with default constructors
  • arviman
    arviman over 9 years
    @Rabbi: You could project an anonymous type as an enumerable and then use a tuple to project in-memory. See more: stackoverflow.com/questions/2118688/return-tuple-from-ef-sel‌​ect
  • Shimmy Weitzhandler
    Shimmy Weitzhandler almost 7 years
    How about ValueTuple?