score:2

You need to "OR" your conditions and specify the comparison each time.

searchedOpps = searchedOpps.Where(s => s.STAGEID == stageAGuid || 
                                       s.STAGEID == stageAPlusGuid || //etc);

Note that consecutive calls to Where will "AND" the conditions together.

searchedOpps = searchedOpps.Where(s => s.STAGEID == stageAGuid);
searchedOpps = searchedOpps.Where(s => s.STAGEID == stageAPlusGuid);

is the same as

searchedOpps = searchedOpps.Where(s => s.STAGEID == stageAGuid && 
                                       s.STAGEID == stageAPlusGuid);

score:6

In your second example, you're looking for objects with STAGEID == stageAGuid AND STAGEID == stageAPlusGuid. It's not surprising you don't get any results.

In your first example you just need to repeat the key you want to compare:

searchedOpps = searchedOpps
               .Where(s => s.STAGEID == stageAGuid 
               || s.STAGEID == stageAPlusGuid [...]);

The way you wrote it, the compiler tries to get a bool by 'ORing' a bool (from the first comparison) and a GUID (the value after the &&).

score:7

You could try this if you have some keys with OR operator, which in C# is ||, for sample:

searchedOpps = searchedOpps.Where(s => s.STAGEID == stageAGuid 
                                    || s.STAGEID == stageAPlusGuid));

But, if you have a list of keys, you could try using the Contains method, for sample:

List<Guid> guids = /* get your keys from somewhere */;
searchedOpps = searchedOpps.Where(s => guids .Contains(s.STAGEID));

If you are working with linq to database (linq-to-sql, entity-framework, nhibernate, etc.) it will generate a query with IN operator with all keys for you.

score:2

You can use the PredicateBuilder class:

var searchPredicate = PredicateBuilder.False<Songs>();

searchPredicate = searchPredicate.Or(g=> g == stageAGuid);
searchPredicate = searchPredicate.Or(g=> g == stageAPlusGuid);

searchedOpps = searchedOpps.Where(searchPredicate);

score:2

You don't say what system you're using for data access, so I'm going to assume it's EF and at least .NET 4.

If you write your query correctly, LINQ-to-Entities will translate it into a SQL "in" clause. Check out this MSDN entry:

http://blogs.msdn.com/b/alexj/archive/2009/03/26/tip-8-writing-where-in-style-queries-using-linq-to-entities.aspx

If you have 7 different guids you're looking for, do something like this:

Guid[] myGuidList =
    { stageAGuid, stageAPlusGuid, anotherGuid, guid4, guid5, guid6, guid7, };

var results = searchedOpps.Where(s => myGuidList.Contains(s.STAGEID)); 

The resulting SQL will look like this:

select * from searchedOpps where STAGEID in (<guid>, <guid>, <guid>...)

Related Articles