C# 如何使用身份列表从实体框架获取 ObjectResult

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/978747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:44:36  来源:igfitidea点击:

How to get ObjectResult from Entity Framework using a list of Identities

c#entity-framework

提问by user120985

I have a HashSet of Identity values that I need to use as the query values to return a ObjectResult from the Entity Framework

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

Here's the HashSet:

这是哈希集:

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

Here's the query that I'm trying to run more or less:

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

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

The "office => office IN officeIds.ToList()" part of the above is what I can't get to work and haven't found any samples on the web for returing objects given a list of primary keys.

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

ctx is the System.Data.Objects.ObjectContext

ctx 是 System.Data.Objects.ObjectContext

采纳答案by Alex James

The examples others have given won't work in the Entity Framework today, because you can't mix client and serverside enumerations in LINQ 2 Entities.

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

Instead you need to build an OR expression, manually.

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

I run a series of EF Tipsand this tipshows you how to build an OR expression up.

我运行了一系列EF 技巧这个技巧向您展示了如何构建 OR 表达式。

Hope this helps

希望这可以帮助

Alex

亚历克斯

回答by Tetraneutron

I don't have any means for checking this currently, but it looks like you are trying seeing if the office object itself is in the list, you probably want to check if its ID is in the list of ID's you have, like

我目前没有任何方法可以检查这个,但看起来您正在尝试查看 office 对象本身是否在列表中,您可能想检查它的 ID 是否在您拥有的 ID 列表中,例如

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

If that doesn't work some indication of what happens when you run your code would be helpful.

如果这不起作用,那么在运行代码时会发生什么情况的一些指示会有所帮助。

回答by Daniel Brückner

Try the following.

请尝试以下操作。

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

But I am not absolutly sure if the Entity Framework supports this query - I tend to believe that you will have to store officeIds.ToList()in a local variable.

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

回答by Devart

There is an alternative way to work around the LINQ to Entities limitation. You can use Entity SQL supporting the IN clause.

有一种替代方法可以解决 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);

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);

回答by Jason Jong

I had similar issue which I resolved via an inner join. See my function below.

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

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;
}

And in your cirsumstances

在你的情况下

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;

回答by Oleksandr Skrypnyk

I have had similar issues a lot of times, another Stack Overflow question with good information is: Most efficient way to get multiple entities by primary key?

我遇到过很多次类似的问题,另一个具有良好信息的 Stack Overflow 问题是:通过主键获取多个实体的最有效方法?

I prefer to use:

我更喜欢使用:

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