1

Eveytime 我想我已经掌握了这些东西。

完全错误:

无法创建类型为“Models.staffnames”的常量值。此上下文仅支持原始类型(“例如 Int32、String 和 Guid”)。

版本:.Net 4 EF 4

我认为这与 DU 和 db.UserProfiles 是 Enumerable 有关,但是当我尝试将其推回以使所有 IQueryable 都发生时,整个事情就爆炸了。就目前而言,在构建支持列表时会发生错误。

有趣的是,这个问题似乎只有在我使用 Roles 提供程序时才会出现。在过去,我只是将它转储到 for 循环中。

任何帮助,将不胜感激。

代码:

IEnumerable<MembershipUser> du = Roles.GetUsersInRole("Dealer").Select(u => Membership.GetUser(u)).AsEnumerable();
        IQueryable<staffnames> du_up = du.Join(db.UserProfiles.AsEnumerable(), mu => (Guid)mu.ProviderUserKey, up => up.UserID, (mu, up) => new staffnames
        {
            UserId = (Guid)mu.ProviderUserKey,
            FirstName = up.FirstName,
            LastName = up.LastName
        }).AsQueryable();
        List<MemberSupportModel> support = db.Wins_support.Where(s=>s.dealerid == uid && !s.closed).Select(s=> new MemberSupportModel{
             aeramanagerack = s.aeramanagerack,
             amgrname = du_up.Where(a => a.UserId == s.aeramanger).Select(a=>a.FirstName+" "+a.LastName).FirstOrDefault(),
             aeramangerackdate = s.aeramangerackdate,
             atypedesc = _atype.Where(wt => wt.Value == s.atypeid).Select(wt => wt.Description).FirstOrDefault(),
             wtypedesc = _wtype.Where(wt => wt.Value == s.typeid).Select(wt => wt.Description).FirstOrDefault(),
             comment =s.comment,
             dateSubmitted = s.dateSubmitted,
             vdate =s.vdate,
             venuename = s.Venues_Logs.Venue.venueName           
        }).ToList();
4

1 回答 1

1

问题出在这条线上amgrname = du_up.Where(...,因为它是内存du_up中非原始类型的集合,因此出现了异常。在这里没有帮助,内存中的内存,你不能改变它。AsQueryable

我会尝试分别从数据库加载您需要的数据,然后将您的du_up集合与从数据库加载的数据集合合并:

// LINQ to Objects in memory
IEnumerable<MembershipUser> du = Roles.GetUsersInRole("Dealer")
    .Select(u => Membership.GetUser(u));

// LINQ to Objects in memory
// db.UserProfiles.AsEnumerable() loads whole UserProfiles table from DB
IEnumerable<staffnames> du_up = du.Join(db.UserProfiles.AsEnumerable(),
    mu => (Guid)mu.ProviderUserKey, up => up.UserID, (mu, up) => new staffnames
    {
        UserId = (Guid)mu.ProviderUserKey,
        FirstName = up.FirstName,
        LastName = up.LastName
    });

// LINQ to Entites = database query
List<MemberSupportModel> support = db.Wins_support
    .Where(s=>s.dealerid == uid && !s.closed)
    .Select(s=> new // anonymous result object
    {
        aeramanagerack = s.aeramanagerack,
        aeramanger = s.aeramanger, // needed later for the du_up Where clause
        aeramangerackdate = s.aeramangerackdate,
        atypedesc = _atype.Where(wt => wt.Value == s.atypeid)
                          .Select(wt => wt.Description)
                          .FirstOrDefault(),
        wtypedesc = _wtype.Where(wt => wt.Value == s.typeid)
                          .Select(wt => wt.Description)
                          .FirstOrDefault(),
        comment = s.comment,
        dateSubmitted = s.dateSubmitted,
        vdate = s.vdate,
        venuename = s.Venues_Logs.Venue.venueName
    })
    .AsEnumerable() // execute database query
    // now LINQ to Objects in memory again
    // Copy values from anonymous result object and select data from du_up
    .Select(s => new MemberSupportModel
    {
        aeramanagerack = s.aeramanagerack,
        amgrname = du_up.Where(a => a.UserId == s.aeramanger)
                        .Select(a => a.FirstName + " " + a.LastName)
                        .FirstOrDefault(),
        aeramangerackdate = s.aeramangerackdate,
        atypedesc = s.atypedesc,
        wtypedesc = s.wtypedesc,
        comment = s.comment,
        dateSubmitted = s.dateSubmitted,
        vdate = s.vdate,
        venuename = s.venuename
    })
    .ToList();
于 2012-07-27T15:39:24.640 回答