【问题标题】:How to get ObjectResult from Entity Framework using a list of Identities如何使用身份列表从实体框架获取 ObjectResult
【发布时间】:2009-06-11 00:18:22
【问题描述】:

我有一个身份值的 HashSet,我需要将其用作查询值以从实体框架返回 ObjectResult

这是哈希集:

HashSet<int> officeIds = new HashSet<int>();

这是我尝试或多或少地运行的查询:

ObjectResult<FilingOffice> offices = ctx.FilingOffice.Where(office => office IN officeIds.ToList());

上面的“office => office IN officeIds.ToList()”部分是我无法开始工作的部分,也没有在网络上找到任何示例来返回给定主键列表的对象。

ctx 是 System.Data.Objects.ObjectContext

【问题讨论】:

    标签: c# entity-framework


    【解决方案1】:

    其他人给出的示例在今天的实体框架中不起作用,因为您不能在 LINQ 2 实体中混合客户端和服务器端枚举。

    相反,您需要手动构建 OR 表达式。

    我运行了一系列 EF Tipsthis tip 向您展示如何构建 OR 表达式。

    希望对你有帮助

    亚历克斯

    【讨论】:

      【解决方案2】:

      我遇到过很多次类似的问题,另一个有很好信息的 Stack Overflow 问题是:Most efficient way to get multiple entities by primary key?

      我更喜欢使用:

      var entities = db.Entities.WhereIn(x => x.Id, ids);
      

      【讨论】:

        【解决方案3】:

        试试下面的。

        var offices = ctx.FilingOffice.Where(o => officeIds.ToList().Contains(o.Id));
        

        但我不确定实体框架是否支持此查询 - 我倾向于认为您必须将 officeIds.ToList() 存储在局部变量中。

        【讨论】:

          【解决方案4】:

          还有一种替代方法可以解决 LINQ to Entities 限制。 您可以使用支持 IN 子句的 Entity SQL。

          string entitySql = String.Format("SELECT VALUE O FROM FilingOffice AS O WHERE O.Id IN {{{0}}}", String.Join(",", officeIds.ToList().ConvertAll(officeId => officeId.ToString()).ToArray()));
          ObjectQuery offices = new ObjectQuery(entitySql, ctx);

          【讨论】:

            【解决方案5】:

            我目前没有任何方法可以检查这一点,但您似乎正在尝试查看办公室对象本身是否在列表中,您可能想检查其 ID 是否在您拥有的 ID 列表中,喜欢

            ObjectResult<FilingOffice> offices = ctx.FilingOffice.Where(office => office.Id IN officeIds.ToList());
            

            如果这不起作用,请说明运行代码时会发生什么。

            【讨论】:

              【解决方案6】:

              我有类似的问题,我通过内部连接解决了。请参阅下面的我的功能。

              public IEnumerable<AccountsCache> GetAccountsById(IEnumerable<int> accountIds)
              {
                  var query =
                      from regAccount in registeredAccounts
                      join Ids in accountIds on regAccount.AccountId equals Ids
                      select regAccount;
                  return query;
              }
              

              在你的情况下

              HashSet<int> officeIds = new HashSet<int>();
              
              ObjectResult<FilingOffice> offices = 
                  from f in ctx.FilingOffice
                  join Ids in officeIds on f.officeId equals Ids
              select f;
              

              【讨论】:

              • 代码没有正确输出: public IEnumerable GetAccountsById(IEnumerable accountIds) { var query = from regAccount in registeredAccounts join Ids in accountIds on regAccount.AccountId 等于 Ids select regAccount;返回查询; } // 还有你的实现 HashSet officeIds = new HashSet(); ObjectResult office = from f in ctx.FilingOffice join Ids in officeIds on f.officeId 等于 Ids select f;
              猜你喜欢
              • 2013-12-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-10-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多