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 Articles
- Using Linq Except not Working as I Thought
- LINQ Source Code Available
- LINQ LEFT JOIN not working on NULL values
- Null coalesce not working in LINQ query
- Linq to Sql .Except not working if null in C#
- LINQ Except not working as expected
- creating Linq to sqlite dbml from DbLinq source code
- linq null refactoring code
- Linq with null parameters not working
- Null reference exception in my LINQ to XML code
- Linq code not working properly
- How to handle nulls in this LINQ Code using a possible null List?
- source code for LINQ 101 samples
- Linq query null check in data source
- Include null cells in Linq query from DB in C# code
- LINQ OUTER JOIN Query - Null in Where clause not working
- C# LINQ code not working for XML parse
- LINQ GroupBy except null values
- Linq Code with Distinct or GroupBy not working
- Using ==Null in Linq not working to find null values
- Attribute of LINQ result is NULL but relationship is working
- c# Linq or code to extract groups from a single list of source data
- Updating List using LINQ working when execute from Immediate window, not from code direct
- Linq Find Partial Text Match - Included code returns duplicate and everything except what it should
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- LINQ order by null column where order is ascending and nulls should be last
- Distinct not working with LINQ to Objects
- Linq code to select one item
- Linq OrderByDescending, null first
- How to define default values with Entity Framework C#?
- Can LINQ based DataGridView's be "disconnected" from the LINQ expression?
- search for a header and put that header in a list
- Concatenating Two Different IEnumerable Objects in C#
- How to Check 2 lists for similar items?
- Get and Add/Update multilevel embedded/nested MongoDB document using C#
- How to filter data in a list using multiple conditions and LINQ?
- Group and Count in Linq issue
- Merging 2 dictionaries having duplicate keys with linq
- How to find point closest to 0,0 point with LINQ and C#
- Asp.net mvc3: How to pull data from database and fill view model in one goal?
- Using Reactives to Merge Chunked Messages
- LINQ: Remove Element from XML based on attribute value?
- Selecting several class properties in one query
- Eager loading with Linq query with restriction on details
- Linq causes syntax error with Top (1)
- What's the equivalent of GROUP INTO in Linq extension method?
- Executing LINQ expression
- Order files in a folder by numerical value instead of string
- How to express Hellinger Distance with Linq