score:1
Accepted answer
You could probably just check for null outside of your expression, like this:
if (LocalWaitingListTypeId > 0 || (GlobalWaitingListTypeId > 0 && StakeBuyInIds != null))
{
if (StakeBuyInIds != null)
{
objWaitingListUser = objWaitingListUser.Where(
x => x.WaitingListTypeId == LocalWaitingListTypeId ||
(x.WaitingListTypeId == GlobalWaitingListTypeId &&
StakeBuyInIds.Contains((Int32)x.StakeBuyInId));
} else {
objWaitingListUser = objWaitingListUser.Where(
x => x.WaitingListTypeId == LocalWaitingListTypeId ||
x.WaitingListTypeId == GlobalWaitingListTypeId);
}
}
You might also be able to do this:
if (LocalWaitingListTypeId > 0 || (GlobalWaitingListTypeId > 0 && StakeBuyInIds != null))
{
var arrayNull = StakeBuyInIds != null;
var array = StakeBuyInIds ?? new int[0];
objWaitingListUser = objWaitingListUser.Where(
x => x.WaitingListTypeId == LocalWaitingListTypeId ||
(x.WaitingListTypeId == GlobalWaitingListTypeId &&
(arrayNotNull || array.Contains((Int32)x.StakeBuyInId)));
}
It effect it tests for null outside of the query, but ensures that it cannot be null when actually executing the query.
score:0
The waitingListTypeId
and stakeBuyinId
should be nullable int
in your relational object WaitingList
.
List<int?> WaitingListTypeIds=new List(new int?[]{1});
var StakeBuyInIds=from w in WaitingListsCollection where new List<int?>(new int?[]{5}).Contains(w.StakeBuyInId) && w.WaitingListTypeId = 2;
var output= from w in WaitingListsCollection where WaitingListTypeIds.Contains(w.WaitingListTypeId) || StakeBuyInIds.Contains(w.StakebuyInId)
Source: stackoverflow.com
Related Query
- How to write below sql query in linq c# where some of paramteres will be null sometimes
- How do I write a linq to sql query to find records where field name can match one of dynamic number of strings?
- How to write linq query to match SQL like select top 100 * from tab?
- How can i write SQL update query with where clause in Entity Framework in C#
- How to write this Linq SQL as a Dynamic Query (using strings)?
- How to write LINQ IN clause query which will work as LIKE operator as well?
- How can I write the following code more elegantly using LINQ query syntax?
- How to write a LEFT OUTER JOIN where the right side is null in LINQ to SQL?
- How to get SQL query into LINQ form in C# code
- How can I check for null values in this linq query where clause
- How to write SQL SELECT INNER JOIN with multiple conditions (with LIKE) query to LINQ to SQL
- How to write linq query from sql query using LINQ?
- C# Linq / How to write below code in single query?
- How to write aggregate query in LINQ reusing part of the select code
- How to write this SQL average query in linq
- how to write a Linq query with a EF code first Many to Many relationship
- How do I write a LINQ query which will generate a JSON string with the following format?
- How To Write Distance LINQ query i have Lat, Long in table with varchar type SQL Server
- write linq similiar where in select from with inner join in sql query
- How to write this SQL query in VB.NET LINQ
- how to write linq query with where clause to get records between 9 am to 5 pm
- How to write OrderByDescending and where clause in single LinQ query
- How to write SQL and LINQ to get the one record from a result with where condition?
- How to write a linq query to exclude some of the records?
- How to write LINQ query to sql IN
- How to write a LINQ query that will return list of entities whose ID is included in specific list?
- LINQ returning Null while same query when write in SQL return result
- How to write a linq query for below Scenerio
- How to write linq query for this sql statement
- How to write this SQL query as a LINQ statement in .NET Core (C#)?
More Query from same tag
- Linq query where in list, performance what is the best?
- use variable within a linq query
- LINQ GroupBy on multiple ref-type fields; Custom EqualityComparer
- How to force XDocument to output the prolog in uppercase while preserving identation and formatting?
- Distinct list<string> of chars
- change order on my object list
- LINQ result form 1:* relationship table -ASP.NET
- How to convert groupby query to linq and compute other thing while executing?
- Adding and removing XDocument elements and their childs with LINQ
- How do I convert a database number to hex when the field might be null?
- Could not determine metatable error binding list to asp.net datagridview
- Does Linq optimizes execution based on real collection type?
- Why Lambda variable scope exists outside LINQ Query?
- LINQ Query to check if a single combo exists out of a list of combos
- Linq on TableCellCollection - C#
- How to create a System.Linq.Expressions.Expression for Like?
- Understanding the basic principle of LINQ's Where/Take
- Query Source could not be identified
- Nullable Expressions
- Linq to Entities performance difference between Expression/Func
- Mongodb c# driver Linq query Group by
- Linq on Complex XML
- Generate All customers total Sales Statement - using Linq
- Linq To SQL Query for counting same records
- Checking for item in Generic List before using it
- How to move string into ViewModel
- Using Lambda expression from a separate class in SELECT clause of LINQ query c# Projection
- Linq to Entities Where Or clause
- how to use variable value in LINQ to entity
- Translate SQL statement with inner joins to LINQ