简体   繁体   中英

return list with anonymous type in entity framework

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();
        }
    }

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.

eg

.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

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();
    }
}

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 eg

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();
        }
    }

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();
    }
}

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;
    }

You can return the type:NameValueCollection or KeyValuePair instead. anonymous type can not be the return type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM