1

可能重复:
LINQ to SQL:返回匿名类型?

linq 连接上的 List<> 使用什么返回类型?我正在加入下面的多个表格,我收到了错误'Cannot implicitly convert type System.Collections.Generic.List<AnonymousType#1> to System.Collections.Generic.List<CargoTanksDAL.aspnet_Users>

    public List<> GetAspnetUsersWithMembershipAndCompany()
    {

        using (DevCargoEntities db = new DevCargoEntities())
        {
            var users = from u in db.aspnet_Users
                        join mem in db.aspnet_Membership on u.UserId equals mem.UserId
                        join cl in db.CT_CompanyLogIn on u.UserName equals cl.UserLogIn
                        join companies in db.CT_Companies on cl.CompanyID equals companies.CompanyID
                        select new
                        {
                            u.UserId,
                            u.UserName,
                            mem.Email,
                            mem.IsLockedOut,
                            mem.IsApproved,
                            mem.CreateDate,
                            companies.CompanyName
                        };

            return users.ToList();
        }
    }
4

2 回答 2

5

没有任何。在这里加入无关紧要,选择很重要,并且您选择匿名类型。您不能返回匿名类型。最好创建一个包含您要返回的属性并实例化该类型的新类。

理论上你可以返回动态或对象,但这是你真的不想走的路径。相信我。

于 2013-01-22T18:32:08.403 回答
2

我会为这个用户开设一个新课程..

public class SomeKindOfUser
{
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public bool IsLockedOut { get; set; }
    public bool IsApproved { get; set; }
    public DateTime CreateDate { get; set; }
    public string CompanyName { get; set; }
}

现在你可以使用

public List<SomeKindOfUser> GetAspnetUsersWithMembershipAndCompany()

然后在选择中:

select new SomeKindOfUser
                        {
                            UserId = u.UserId,
                            UserName = u.UserName,
                            Email = mem.Email,
                            IsLockedOut = mem.IsLockedOut,
                            IsApproved = mem.IsApproved,
                            CreateDate = mem.CreateDate,
                            CompanyName = companies.CompanyName
                        };
于 2013-01-22T18:35:37.827 回答