Andrew Kilburn
Andrew Kilburn

Reputation: 2251

Foreach in LINQ statement, is this possible?

I have a list of integers. I am running a repository to retrieve data, however. I need to do a where on the list of integers. I am wondering how I can go about this and whether it is even possible. For example I need to do something like this:

join h in camOnlineDb.Headers
                       on new { d.ClientID, d.ClaimID }
                   equals new { h.ClientID, h.ClaimID }
                   where d.ClaimID == foreach(var item in IntList) {
                       d.ClaimId == item.ClaimID
                   }    

I'm know the syntax isn't right but if it was, I wouldn't be here! So i'm wondering if this is possible and if not, what is the best way to go around this?

Upvotes: 0

Views: 94

Answers (2)

Backs
Backs

Reputation: 24903

Try linq method All

join h in camOnlineDb.Headers
    on new { d.ClientID, d.ClaimID }
    equals new { h.ClientID, h.ClaimID }
where IntList.All(item => d.ClaimId == item.ClaimID) 

Or Any:

join h in camOnlineDb.Headers
    on new { d.ClientID, d.ClaimID }
    equals new { h.ClientID, h.ClaimID }
where IntList.Any(item => d.ClaimId == item.ClaimID) 

So, difference is when all elements of IntList must match d.ClaimId use All, if you need at least one - use Any.

Upvotes: 0

xeraphim
xeraphim

Reputation: 4645

if I understand your questions correctly, you can use the following syntax

join h in camOnlineDb.Headers
                   on new { d.ClientID, d.ClaimID }
               equals new { h.ClientID, h.ClaimID }
               where IntList.Select(x => x.ClaimID).Contains(d.ClaimID)

Upvotes: 2

Related Questions