return list with anonymous type in entity framework

后端 未结 6 1496
北恋
北恋 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: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 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 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();
        }
    }
    

提交回复
热议问题