在实体框架匿名类型返回列表 [英] return list with anonymous type in entity framework

查看:114
本文介绍了在实体框架匿名类型返回列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能与匿名类型返回列表,因为这个code,我得到

的类型或命名空间名称'T'找不到(是否缺少using指令或程序集引用?)

我只需要返回IdMember和用户名,谢谢

 公共静态列表< T> GetMembersItems(字符串ProjectGuid)
    {
        使用(PMEntities上下文=新PMEntities(NAME = PMEntities))
        {
            VAR项目= context.Knowledge_Project_Members.Include(Knowledge_Project)。包括(Profile_Information)
                        。凡(P => p.Knowledge_Project.Guid == ProjectGuid)
                        。选择(行=>新建{IdMember = row.IdMember,用户名= row.Profile_Information.UserName});            返回items.ToList();
        }
    }


解决方案

正如雅各说,你不能返回一个匿名类型,但是你可以做一个新的类,并使用它的select语句内。下面是LINQ to SQL的一个类似的问题。同样的方法可以适用于实体框架。

LinqToSql匿名类型

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

解决方案

As Jacob said you can't return an anonymous type but you can make a new class and use it inside of your select statement. Here is a similar question for Linq To Sql. The same method can apply to entity framework.

LinqToSql Anonymous Type

这篇关于在实体框架匿名类型返回列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆