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 'OR
ing' 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:
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>...)
Source: stackoverflow.com
Related Articles
- Lambda expression for multiple Guids
- Lambda expression for multiple parameters
- How Build Lambda Expression Tree with multiple conditions
- lambda expression join multiple tables with select and where clause
- Selecting multiple columns with linq query and lambda expression
- Linq - Using array in Lambda expression to fetch multiple records
- C# multiple variables in lambda expression inside LinQ query
- How to reuse a linq expression for 'Where' when using multiple source tables
- LINQ lambda expression to replace multiple characters in a string?
- Multiple Field Join via Lambda Expression
- Lambda Expression for Many to Many realtionship in C# EF 5 Code First
- Left join multiple tables using lambda expression
- Lambda expression creates multiple queries
- How to write the same code using Lambda Expression
- How to write following code in lambda expression or linq?
- Select multiple fields in lambda expression
- Max of 2 columns with multiple group by lambda expression entity framework
- How can I make a Multiple property lambda expression with Linq
- How to filter entity framework result with multiple columns using a lambda expression
- How to return multiple rows using LINQ lambda expression from SQL Server 2014?
- ForEach loop with Lambda expression in Razor code MVC 5 For List<T>
- VS Code Coverage won't recognize only possible Expression Lambda Path
- MVC4 - Linq Lambda expression to query multiple association tables
- using Single/First methods to return multiple values in linq or lambda expression
- Multiple join with Lambda Expression
- Split a list into multiple lists using lambda expression
- Lambda expression with statement body error in previously working code
- LINQ Lambda Expression (Fluent Programming) to retrieve an average of multiple occurrences
- C# Lambda Expression for selecting multiple fields
- How to make a Lambda expression Reusable in multiple LINQ Queries
- Entity Framework average, group by query
- Linq to Sql problem
- Contains on list is too slow, how to improve?
- Table selection
- Using LINQ to Group By List<string> Values
- What can be used instead of Datatable in LINQ
- Linq To SQL Attach/Refresh Entity Object
- Do Compiled Queries Cache?
- Task.Any() Call task twice
- SQL/ Linq query select from many to many
- Efficiently filtering a large collection of POCO entities
- LINQ to XML (C#) Iterate through nodes to build string?
- EF Core how to rewrite the query in a form that can be translated
- Linq query with a join and a where clause giving an error
- Incrementing an element using LINQ to XML
- How to get enumerable range with a variable step count
- why Linq to sql can't track update information if we directly assign new object value to old .?
- Need help to sorting an array in a complicated way c# linq
- LINQ/C# - Making a DTO from a collection?
- Object not deleting using remove in Enity Framework