return list with anonymous type in entity framework

后端 未结 6 1470
北恋
北恋 2021-01-02 07:21

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 a

相关标签:
6条回答
  • 2021-01-02 07:53

    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();
            }
        }
    
    0 讨论(0)
  • 2021-01-02 07:54

    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();
        }
    }
    
    0 讨论(0)
  • 2021-01-02 07:58

    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
    
    0 讨论(0)
  • 2021-01-02 08:03

    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();
        }
    }
    
    0 讨论(0)
  • 2021-01-02 08:08

    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;
        }
    
    0 讨论(0)
  • 2021-01-02 08:16

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

    0 讨论(0)
提交回复
热议问题