24

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:

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.

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

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'."

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

secretformula
  • 6,414
  • 3
  • 33
  • 56
Matt Dell
  • 9,205
  • 11
  • 41
  • 58

4 Answers4

47

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..

where OrgID in (1,2,...,n)
Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • 1
    Yes sir! You are correct. I, of course, figured this out about 2 minutes after I posted this (which I posted in frustration after 2 hours of attempts) Anyway, I ended up making the int[] a List. Not sure if that affects the above solution or not. But that's how I got it to work. – Matt Dell May 07 '09 at 19:32
  • 1
    This saved me a double foreach loop – The Muffin Man Jun 04 '11 at 01:18
  • Cool, can this be done using the object syntax instead of query syntax? – TheLegendaryCopyCoder Apr 18 '14 at 09:21
  • 1
    @TheLegendaryCopyCoder: By "object syntax" do you mean using the extension methods directly instead of using the query syntax? If so, sure. `var query = this.Database.Personnels.Where(p => OrgIds.Contains(p.OrgID)).ToList();` – Adam Robinson Apr 18 '14 at 12:18
7

A check using the Contains method should do the job here.

var query = (from p in this.Database.Personnels
             where OrgIds.Contains(p.OrganisationId)
             select p).ToList();
Noldorin
  • 144,213
  • 56
  • 264
  • 302
2

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

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 Dell
  • 9,205
  • 11
  • 41
  • 58
1

It would be something like this, 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.

Solmead
  • 4,158
  • 2
  • 26
  • 30