How to check available hotel rooms - c#

I am making a hotel reservation system in windows form by using SQL with LINQ. I have tables such as reservation tabel and room table. In reservation table I have checkin and checkout dates. I have no problem to show those rooms from reservation table in a specific date, but I don't know how to show rooms that are available on specific dates. I don't how to compare or filter out RoomsId from reservation table and room table.
Here is how I am getting rooms from reservation table on a specific date (checkin and checkout):
DateTime StartDateWantToBook = Convert.ToDateTime(dateTimePicker1.Value.ToString());
DateTime EndDateWantToBook = Convert.ToDateTime(dateTimePicker2.Value.ToString());
var ReservedRooms = (from u in db.Room join b in db.Reservation on u.RoomId equals b.RoomId join f in db.Floor on u.FloorId equals f.FloorId join ty in db.RoomType on u.RoomTypeId equals ty.RoomTypeId where StartDateWantToBook <= b.EndDate && b.StartDate <= EndDateWantToBook
select new {
RomId = b.RoomId,
Floor = f.FloorName,
RommsNr = u.RumNummer,
Room_Type = ty.AmountRomms
// But Here by somehow I think I have to run
// another Linq query to filter RoomsId and show only those who do not exists in Reservation table.
}
).ToList();
dataGridView1.DataSource = ReservedRooms;
So the question is How to show all rooms in Rooms table that do not exist in Reservartionroom in a specific dates. Thank you again!

This must work for you.
var AvailebleRooms = (from u in db.Room
where (!u.Reservation.Any(b => b.EndDate >= StartDateWantToBook && b.StartDate <= EndDateWantToBook))
select u).ToList();

i think you need left outer join AllRooms and ReservedRooms to get rooms only using where clause with specific dates

You need to select AllRooms, ReservedRooms in a specific date and filter AllRoom by list reservartion. That's my Idea, maybe contains is wrong.
DateTime StartDateWantToBook = Convert.ToDateTime(dateTimePicker1.Value.ToString());
DateTime EndDateWantToBook = Convert.ToDateTime(dateTimePicker2.Value.ToString());
var AllRooms = (from u in db.Room join b in db.Reservation on u.RoomId equals b.RoomId join f in db.Floor on u.FloorId equals f.FloorId join ty in db.RoomType on u.RoomTypeId equals ty.RoomTypeId select new {
RomId = b.RoomId,
Floor = f.FloorName,
RommsNr = u.RumNummer,
Room_Type = ty.AmountRomms
// But Here by somehow I think I have to run
// another Linq query to filter RoomsId and show only those who do not exists in Reservation table.
}).ToList();
var ReservedRooms = (from u in db.Room join b in db.Reservation on u.RoomId equals b.RoomId join f in db.Floor on u.FloorId equals f.FloorId join ty in db.RoomType on u.RoomTypeId equals ty.RoomTypeId where StartDateWantToBook <= b.EndDate && b.StartDate <= EndDateWantToBook select new {
RomId = b.RoomId,
Floor = f.FloorName,
RommsNr = u.RumNummer,
Room_Type = ty.AmountRomms
// But Here by somehow I think I have to run
// another Linq query to filter RoomsId and show only those who do not exists in Reservation table.
}).ToList();
var result = AllRoom.Except(ReservedRooms);
dataGridView1.DataSource = ReservedRooms;

Related

How to add less than or equal to condition in linq inner join

We have two objects, Dates and ActiveEvents. Want to perform inner join on these with less than or equal to condition in linq. Same as ref of below SQL where consider #Tables are C# objects
Select A. from #Activities A
Inner Join #Dates D ON A.ActivityDate <= D.ProcessDate
Tried with below but it's not giving correct results.
var filteredActivity = (from e in ActiveEvents
from p in dates
where e.ActivityDate <= p.Date
select new ActiveEvent
{
ActivityDate = p.Date,
EventId = e.EventId
}).ToList();
And
var filteredActivity = (from e in ActiveEvents
from p in dates.Where(r => e.ActivityDate <= r)
select new ActiveEvent
{
ActivityDate = p.Date,
EventId = e.EventId
}).ToList();
Can you please suggest any better way to do this?
You can try this way
var filteredActivity = (from e in ActiveEvents
join p in dates
where e.ActivityDate <= p.ProcessDate
select new ActiveEvent
{
ActivityDate = p.Date,
EventId = e.EventId
}).ToList();
P/s: Ideally, between 2 tables should contain the foreign key to join like this join p in dates on e.Key equals p.ForeignKey
Based on your example, the query is filtering on ProcessDate but your linq query is filtering on p.Date. Are those the same field? The first example you gave should be correct.

How to perform a left join with an additional filtering in LINQ to entities?

I have several tables, the main one is called DefectRecord, others are called DefectArea, DefectLevel...etc and the one called DefectAttachment. And this problem is about joining DefectRecord with other tables to get a ViewModel for further use. What the hard part I am facing is about the DefectAttachment table.
DefectRecord has a 1-to-many relation with DefectAttachment. While there may be NO attachment at all for one defect record, there may be multiple attachments.
Logically I tried to perform a left join among DefectRecord & DefectAttachment, but there is one more requiredment:
If there is multiple attachments, select ONLY the oldest one(i.e. the
one with oldest CreatedDate field value)
I am stuck at this requirement, how can I perform this with LINQ-to-Entities? Below is the code of what I have now:
var ret = (from dr in defectRecordQuery
join ft in filterQuery on dr.FilterID equals ft.FilterID
join l in levelQuery on dr.LevelID equals l.LevelID
join a in attachmentQuery on dr.DefectRecordID equals a.DefectRecordID into drd
from g in drd.DefaultIfEmpty()
select new DefectRecordViewModel
{
DefectRecordCode = dr.Code,
DefectAttachmentContent = g == null ? null : g.FileContent,
LookupFilterName = ft.FilterName,
}).ToList();
The *Query variable are the IQueryable object which get the full list of corresponding table.
Group your results by the Code and FilterName and then for the content take that of the item in the group that has the oldest date
var ret = (from dr in defectRecordQuery
join ft in filterQuery on dr.FilterID equals ft.FilterID
join l in levelQuery on dr.LevelID equals l.LevelID
join d in attachmentQuery on dr.DefectRecordID equals d.DefectRecordID into drd
from g in drd.DefaultIfEmpty()
group g by new { dr.Code, ft.FilterName } into gg
select new DefectRecordViewModel
{
DefectRecordCode = gg.Key.Code,
DefectAttachmentContent = gg.OrderByDescending(x => x.CreateDateTime).FirstOrDefault() == null? null: gg.OrderByDescending(x => x.CreateDateTime).FirstOrDefault().FileContent,
LookupFilterName = gg.Key.FilterName,
}).ToList();
If using C# 6.0 or higher then you can do:
DefectAttachmentContent = gg.OrderByDescending(x => x.CreateDateTime)
.FirstOrDefault()?.FileContent,

Subquery in Linq query giving A query body must end with a select clause or a group clause

booked - below - should be the sum of the NumberBooked column from the Bookings table - which has a link to the TourDates table on the TourDateId.
However I'm getting the error A query body must end with a select clause or a group clause
Can anyone please help me fix this query?
Thank you,
Mark
var tours = from t in Tours
join d in TourDates on t.TourId equals d.TourId
where d.Date == dt
select new
{
t.TourId,
d.TourDateId,
booked = (from b in Bookings where d.TourDateId == b.TourDateId)
Select new {bk.Sum(b.NumberBooked()}
};
I believe this:
booked = (from b in Bookings where d.TourDateId == b.TourDateId) // oops
Select new {bk.Sum(b.NumberBooked()}
should be this:
booked = (from b in Bookings where d.TourDateId == b.TourDateId // move from here
select new {bk.Sum(b.NumberBooked()}) // to here
Note that I moved the end parenthesis ) so that it comes after the select, not after TourDateId
The closing round paranthesis ends the query which needs a select at the end.
Why don't you use method syntax? It is much better readable in this case. Also, a Select is optional with .Where and method syntax:
join d in TourDates on t.TourId equals d.TourId
where d.Date == dt
select new
{
t.TourId,
d.TourDateId,
booked = Bookings.Where(b => d.TourDateId == b.TourDateId)
.Sum(b => b.NumberBooked())
};
Note that i have removed the anonymous type since you just want the sum of that column
should be the sum of the NumberBooked column from the Bookings table -
which has a link to the TourDates table on the TourDateId.

linq-sql join two tables and select columns

I'm new to sql-linq and I'm trying to join two tables using their common id which is motherid. I have done that but when I try to convert the returned value into list it throws an exception saying "The query contains references to items defined on a different data context." Here is the code.
var todaySecondVisitProfile = (from a in _maternalvisitvaluedb.Value
join b in _maternalcarevaluedb.Value on a.MotherId equals b.MotherID
where (DateTime)a.SecondVisit.Date == DateTime.Now.Date
select new
{
FirstName = b.FirstName,
LastName = b.LastName,
PhoneNo = b.PhoneNo
}).ToList();
If I can't convert the result into list how can I access my result? tnx for the help.
I think you are trying to do a Linq on two different databases. If so then you should so something like this:
var firstQuery = (from s in _maternalvisitvaluedb.Value select s).ToList();
var secondQuery = (from t in _maternalcarevaluedb.Value select t).ToList();
var result = (from s in firstQuery
join k in secondQuery
on s.MotherId equals k.MotherId
where (DateTime)s.SecondVisit.Date == DateTime.Now.Date
select s).ToList();

have join multiple table and get the output in DataRow in LINQ

HI,
I have a scenario where i have join multiple table and get the output in DataRow(All the Rows return by the query).
SQL Query:
SELECT Fr.InterCodeId
FROM
CodeShareInterline Fr,
Airline A,Zone Z #
WHERE
A.AirlineId = Fr.AirlineId
And Fr.ContractId=Z.ContractId
I know how to perform join in LINQ but how can i select all the column(Rows) in select statement of LINQ.
var result = from fr in dataContext.CodeShareInterline
from a in dataContext.AirLine
from z in dataContext.Zone
where a.AirlineId == fr.AirlineId && fr.ContractId == z.ContractId
select new
{
Interline = fr,
AirLine = a,
Zone = z
};
The anonymous type contains all data you want, you can easily visit one column by:
result.FirstOrDefault().Zone.SomeField
This is untested but something close to this should work. Assuming your data context is call Context. This is a translation of what you have above.
var o = from fr in Context.CodeShareInterline
join a from Context.Airline on a.AirlineId == fr.AirlineId
join z from Context.Zone on fr.ContactId == z.ContactId
select fr.InterCodeId;
If you want to select all of the data then you need to do something like this.
var o = from fr in Context.CodeShareInterline
join a from Context.Airline on a.AirlineId == fr.AirlineId
join z from Context.Zone on fr.ContactId == z.ContactId
select new {
Interline = fr,
AirLine = a,
Zone = z
};

Categories