Linq to Entities查询的问题

时间:2011-06-13 03:19:19

标签: linq-to-entities

首先,我要感谢你在这件事上的任何帮助。 我正在做这个查询(Linq to Entities)

public override IEnumerable Execute(UserGroup userGroup)         {

        return from a in _ctx.Activities.OfType<ResourceActivity>()
               where userGroup.Users.Contains(a.User)
               group a by new { a.User, a.Href } into fg
               select new UserResourceActivityReturnValue
               {
                   UserDBIdentity = fg.Key.User.DbId,
                   UserIdentity = fg.Key.User.Id,
                   UserName = fg.Key.User.Name,
                   ResourceName = fg.Key.Href,
                   ResourceAccess = fg.Count()
               };

我有这个错误:

“无法创建类型'Data.User'的常量值。在此上下文中仅支持基本类型(例如Int32,String和Guid')。”

所以我把它改成了这个:

public override IEnumerable Execute(UserGroup userGroup)         {

        return from a in _ctx.Activities.OfType<ResourceActivity>()
               from u in userGroup.Users
               where a.User.DbId == u.DbId
               group a by new { a.User, a.Href } into fg
               select new UserResourceActivityReturnValue
               {
                   UserDBIdentity = fg.Key.User.DbId,
                   UserIdentity = fg.Key.User.Id,
                   UserName = fg.Key.User.Name,
                   ResourceName = fg.Key.Href,
                   ResourceAccess = fg.Count()
               };

我一直看到同样的错误,请帮助。

我的电子邮件是aalbalat@uclv.edu.cu

非常感谢

1 个答案:

答案 0 :(得分:0)

原始查询的修正:

return from a in _ctx.Activities.OfType<ResourceActivity>()
        where userGroup.Users.Any(arg => arg.DbId == a.User.DbId)
        group a by 
            new
            {
                UserId = a.User.Id, 
                UserDbId = a.User.DbId, 
                UserName =  a.User.Name, 
                a.Href
            } into fg
        select new UserResourceActivityReturnValue
        {
            UserDBIdentity = fg.Key.UserDbId,
            UserIdentity = fg.Key.UserId,
            UserName = fg.Key.User.UserName,
            ResourceName = fg.Key.Href,
            ResourceAccess = fg.Count()
        };

问题是您在UserContains中使用GroupBy实体。