Lambda expression for multiple guides

Attempting to create a Lambda expression to select records from a database that contains any of seven Guid values.

When I try the following:

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

      

I get Operator '&&' cannot be applied to operands of type 'bool' and 'System.Guid'

, the same for ||

(not sure if or if I should use any of these operators)

If I try:

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

      

Returns nothing

Does anyone help me with this?

+3


source to share


5 answers


In your second example, you are looking for STAGEID == stageAGuid

AND objects STAGEID == stageAPlusGuid

. It's no surprise that you won't get any results.

In the first example, you just need to iterate over the key you want to compare:



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

      

As you wrote it, the compiler tries to get bool

by ' OR

ing' a bool

(from first comparison) and GUID

(value after &&

).

+6


source


You can try this if you have multiple keys with an operator OR

that is equal in C # ||

, for example:

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

      

But if you have a list of keys, you can try using the Contains

sample method :



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

      

If you are working with linq in a database (linq-to-sql, entity-framework, nhibernate, etc.) it will generate a statement IN

with all the keys for you.

+6


source


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 be AND together.

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

      

coincides with

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

      

+2


source


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);

      

+2


source


You don't say which system you are using for data access, so I will assume it is EF and at least .NET 4.

If you write your query correctly, LINQ-to-Entities will translate it into an "in" SQL 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 queries you are looking for, do 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>...)

      

+1


source







All Articles