linq2sql:不允许在查询中显式构造实体类型'#some type#' - 也使用join

时间:2009-09-22 08:14:49

标签: linq-to-sql inner-join

我遇到了问题映射。我正在阅读scottGU的“数据整形功能”帖子 - http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx

但我试过这个

      IQueryable<AccessRights> accessRights = 
                            from t1 in this.db.AccessRights
                            join t2 in this.db.AccessRightsExtra
                            on t1.IdAccessRights equals t2.IdAccessRights 
                            where t2.IdUser== userId
                            select new AccessRights
                            {
                                IdAccessRights = t1.IdAccessRights,
                                Description= t2.Description
                            };

但产生此错误“不允许在查询中显式构造实体类型'#some type#'”

根据上面链接中的scottgus帖子,我也尝试过(在选择新内容后注意缺少类型)

     IQueryable<AccessRights> accessRights = 
                            from t1 in this.db.AccessRights
                            join t2 in this.db.AccessRightsExtra
                            on t1.IdAccessRights equals t2.IdAccessRights 
                            where t2.IdUser== userId
                            select new
                            {
                                IdAccessRights = t1.IdAccessRights,
                                Description= t2.Description
                            };

但这会产生

无法将类型'System.Linq.IQueryable'隐式转换为'System.Linq.IQueryable'。存在显式转换(您是否错过了演员?)

非常感谢任何人的洞察力。

2 个答案:

答案 0 :(得分:4)

怎么样:

 IEnumerable<AccessRights> accessRights = 
    // This bit works in the database
    (from t1 in this.db.AccessRights
    join t2 in this.db.AccessRightsExtra
    on t1.IdAccessRights equals t2.IdAccessRights 
    where t2.IdUser== userId
    select new
    {
        IdAccessRights = t1.IdAccessRights,
        Description= t2.Description
    })
    .AsEnumerable() // From here on it's in-process
    .Select(x => new AccessRights
            {
                IdAccessRights = x.IdAccessRights,
                Description= x.Description
            });

请注意,结果为IEnumerable<T>而不是IQueryable<T>,但仍会进行延迟评估。这会导致问题吗?

或者,只需使用隐式类型的局部变量来使用匿名类型:

var accessRights = from t1 in this.db.AccessRights
                   join t2 in this.db.AccessRightsExtra
                   on t1.IdAccessRights equals t2.IdAccessRights 
                   where t2.IdUser== userId
                   select new
                   {
                       IdAccessRights = t1.IdAccessRights,
                       Description= t2.Description
                   };

这仍然是IQueryable<T>,但其中T是匿名类型。您将无法将其用作IQueryable<AccessRights>,但如果您需要的只是IdAccessRightsDescription属性,并且您只需要使用相同的方法,那么它可能会很好足够你...

答案 1 :(得分:0)

如果您创建一个没有[Table] -Attribute的新派生类,可能会这样:

public class AccessRightsLocal : AccessRights
{
}

然后,您可以在select中创建“AccessRightsLocal”的实例。将新实例转换为“AccessRights”,它将起作用!

IQueryable<AccessRights> accessRights = 
                    from t1 in this.db.AccessRights
                    join t2 in this.db.AccessRightsExtra
                    on t1.IdAccessRights equals t2.IdAccessRights 
                    where t2.IdUser== userId
                    select (AccessRights) new AccessRightsLocal
                    {
                        IdAccessRights = t1.IdAccessRights,
                        Description= t2.Description
                    };