SQL 在 where 子句中使用 Array 进行 Linq 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/836491/
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-09-01 01:58:29  来源:igfitidea点击:

Linq query with Array in where clause?

.netsqllinq

提问by Matt Dell

I have searched for this, but still can't seem to get this to work for me. I have an array of Id's associated with a user (their Organization Id). These are placed in an int[] as follows:

我已经搜索过这个,但似乎仍然无法让它对我来说有效。我有一组与用户相关联的 ID(他们的组织 ID)。它们被放置在一个 int[] 中,如下所示:

int[] OrgIds = (from oh in this.Database.OrganizationsHierarchies
                       join o in this.Database.Organizations on oh.OrganizationsId equals o.Id
                       where (oh.Hierarchy.Contains(@OrgId))
                          || (oh.OrganizationsId == Id)
                       select o.Id).ToArray();

The code there isn't very important, but it shows that I am getting an integer array from a Linq query.

那里的代码不是很重要,但它表明我从 Linq 查询中获取了一个整数数组。

From this, though, I want to run another Linq query that gets a list of Personnel, that code is as follows:

尽管如此,我想运行另一个获取人员列表的 Linq 查询,代码如下:

List<Personnel> query = (from p in this.Database.Personnels
                                where (search the array)
                                select p).ToList();

I want to add in the where clause a way to select only the users with the OrganizationId's in the array. So, in SQL where I would do something like "where OrganizationId = '12' or OrganizationId = '13' or OrganizatonId = '17'."

我想在 where 子句中添加一种仅选择数组中具有 OrganizationId 的用户的方法。因此,在 SQL 中,我会执行诸如“where OrganizationId = '12' 或 OrganizationId = '13' 或 OrganizatonId = '17'”之类的操作。

Can I do this fairly easily in Linq / .NET?

我可以在 Linq / .NET 中轻松地做到这一点吗?

回答by Adam Robinson

While this is probably better suited to a join, you can use this:

虽然这可能更适合连接,但您可以使用它:

List<Personnel> query = 
    (from p in this.Database.Personnels 
    where OrgIds.Contains(p.OrgID) select p).ToList();

This will translate into SQL something like..

这将转换为 SQL 之类的东西。

where OrgID in (1,2,...,n)

回答by Noldorin

A check using the Containsmethod should do the job here.

使用该Contains方法的检查应该在这里完成工作。

var query = (from p in this.Database.Personnels
             where OrgIds.Contains(p.OrganisationId)
             select p).ToList();

回答by Matt Dell

I wanted to give Adam credit for the answer, but I also wanted to share the code I used to make this work:

我想感谢 Adam 的回答,但我也想分享我用来完成这项工作的代码:

List<int> OrgIds= (from oh in this.Database.OrganizationsHierarchies
                       join o in this.Database.Organizations on oh.OrganizationsId equals o.Id
                       where (oh.Hierarchy.Contains(@OrgId))
                          || (oh.OrganizationsId == Id)
                       select o.Id).ToList();

List<Personnel> query = (from p in this.Database.Personnels
                                where (OrgIds.Contains(p.OrganizationId))
                                select p).ToList();

Thanks all,

谢谢大家,

-Matt

-马特

回答by Solmead

It would be something like this, OrgIds.ToList.Contains(p.OrginizationID)

它会是这样的, OrgIds.ToList.Contains(p.OrginizationID)

Though really I would do it more like this:

虽然我真的会这样做:

var OrgIds = (from oh in this.Database.OrganizationsHierarchies
                   join o in this.Database.Organizations on oh.OrganizationsId equals o.Id
                   where (oh.Hierarchy.Contains(@OrgId))
                      || (oh.OrganizationsId == Id)
                   select o.Id);
List<Personnel> query = (from p in this.Database.Personnels
                            where (OrgIds.Contains(p.OrigizationID)
                            select p).ToList();

That way the final query to get personnel will execute containing the combined query from both.

这样,获取人员的最终查询将包含来自两者的组合查询。