score:1
Accepted answer
Can't you just resign from Except
and write:
var rooms = dbt.Rooms.Where(r => r.h_id == AccID)
.Where(room => !room.Rentals.Any(rental =>
(dteFrom >= rental.check_in && dteFrom < rental.check_out))).
.GroupBy(p => p.RoomTypes).Select(g => new RatesViewModel
{
TypeName = g.Key.t_name,
TypeID = g.Key.t_id,
TypeCount = g.Count()
})
.ToList();
score:0
Try using the null coalescing operator: ??
and then getting the IQueryable<T>
equivalent of Enumerable<T>.Empty
, which is Enumerable<T>.Empty().AsQueryable()
and DefaultIfEmpty
with an invalid room, so it doesn't go crazy about it being null || empty:
var rooms = dbt.Rooms.Where(r => r.h_id == AccID)
.Except(prebookedRooms ?? Enumerable.Empty<T>().AsQueryable().DefualtIfEmpty(new Room() { room_id = -1, hotel_id = -1, type_id = -1}))
.GroupBy(p => p.RoomTypes).Select(g => new RatesViewModel
{
TypeName = g.Key.t_name,
TypeID = g.Key.t_id,
TypeCount = g.Count()
})
.ToList();
I don't know your type, so I don't know what to put in the Enumerable.Empty<T>
call's generic parameter. Add that yourself.
Also, remember to be using System.Linq
!
Source: stackoverflow.com
Related Query
- Linq to Sql .Except not working if null in C#
- Using Linq Except not Working as I Thought
- LINQ SQL query check if a object field is not null
- LINQ LEFT JOIN not working on NULL values
- Linq to SQL update not working using Repository pattern
- Null coalesce not working in LINQ query
- Where Not In OR Except simulation of SQL in LINQ to Objects
- Converting SQL query into LINQ - not working
- LINQ Except not working as expected
- Linq Sql Int null equal to null. Not returning rows
- SQL Query to LINQ for MVC with SUM and IS NOT NULL
- Linq to Sql isnull in where clause not working
- SQL to LINQ Query with date in where clause is not working
- How do I check if a SQL Server 2005 TEXT column is not null or empty using LINQ To Entities?
- Linq with null parameters not working
- Regex is not working with Linq to Sql
- Distinct in LINQ to SQL is not working as expected(at all)
- Why isn't this short circuit is not working in linq to sql query?
- Linq code not working properly
- Linq query is not working while sql is working properly
- LINQ to SQL - Expression checking for null could not be translated
- Convert SQL query to LINQ query not working
- LINQ OUTER JOIN Query - Null in Where clause not working
- Gridview sorting not working with Linq to SQL
- convert SQL to LINQ not working
- Linq to SQL "IN" Clause not working
- C# LINQ code not working for XML parse
- Oracle SQL statement to Linq Not working
- Linq Code with Distinct or GroupBy not working
- Using ==Null in Linq not working to find null values
More Query from same tag
- Adding LambaExpression to an instance of IQueryable
- Convert complex SQL query to LINQ for rest api
- Optimising Searching of a 2-dimensional Array with LINQ
- Selecting from list based on multiple unique fields
- Query which returns results only if all (non-primitive) values from a compared enumerable are contained in the target enumerable
- LINQ - Writing an extension method to get the row with maximum value for each group
- If and else with LINQ
- LINQ and PagedList how to orderby
- LINQ: How to rewrite WHERE clause to avoid: Local sequence cannot be used in LINQ to SQL
- Copy subset of items from one RadComboBox to another
- OrderBy specific elements in List
- How to restrict LINQ query to marketing lists of a given campaign?
- How to convert a foreach within a foreach to LINQ, if it's possible
- Can I use LINQ to retrieve only "on change" values?
- Obtaining BreezeJS Predicate equivalent on the Server Side
- How to get object field for LINQ cached object?
- Entity framework - how to filter eager loaded navigational/relational properties?
- Passing Func to method where the func needs parameters from inside method
- Is a statement recalculated in every iteration when used in LINQ?
- Concatenating strings in two lists to create a third list
- LINQ and XML with C#
- Split, group and count string
- Composite iteration failure (.net)
- Index of List containing dictionaries of strings based on dictionary values
- Why is there no compiler warning for LINQ queries that are not assigned to variables
- Get an ID if two other ID matches in two different tables
- Entity framework 5.0 transaction in buissnes logic layer
- C# SQL query blocks server memory
- Why is FindIndex in my LINQ expression not working?
- Help sorting XML Data in .NET (maybe with LINQ query?)