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:
但是,我希望运行另一个获取Personnel列表的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中,我会做“像OrganizationId ='12'或OrganizationId ='13'或OrganizatonId = '17'的事情。”
Can I do this fairly easily in Linq / .NET?
我可以在Linq / .NET中相当容易地做到这一点吗?
4 个解决方案
#1
36
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)
#2
5
A check using the Contains
method should do the job here.
使用Contains方法检查应该在这里完成工作。
var query = (from p in this.Database.Personnels
where OrgIds.Contains(p.OrganisationId)
select p).ToList();
#3
2
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
-Matt
#4
1
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.
这样,获取人员的最终查询将包含来自两者的组合查询。
#1
36
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)
#2
5
A check using the Contains
method should do the job here.
使用Contains方法检查应该在这里完成工作。
var query = (from p in this.Database.Personnels
where OrgIds.Contains(p.OrganisationId)
select p).ToList();
#3
2
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
-Matt
#4
1
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.
这样,获取人员的最终查询将包含来自两者的组合查询。