Left outer join with eagar loading - linq

I am writing a linq having a left outer join with conditions on right side table.
my code
var leftHotelRooms = db.HotelRooms.Include(hr=>hr.HotelRoomBlackoutPeriods)
.Where(room => hotelCodes.Contains(room.UserHotelId) &&
room.Scope == scope &&
room.IsDeleted == false &&
room.IsEnabled == true &&
room.MaxCapacity >= minimumNumberOfGuests)
.GroupJoin(db.HotelRoomBlackoutPeriods,
room => room.Id,
blackoutPeriod => blackoutPeriod.HotelRoomId,
(room, blackoutPeriod) => new
{
room,
blackoutPeriods = blackoutPeriod.DefaultIfEmpty()
})
.Select(a => new {a.room,a.blackoutPeriods})
.Where(
x => x.blackoutPeriods.Any(y => DbFunctions.DiffDays(y.StartDate, checkIn) != 0
&& (DbFunctions.DiffDays(y.StartDate, checkIn) < 0) ? (DbFunctions.DiffDays(y.StartDate, checkOut) >= 0 ? false : true) : true &&
(DbFunctions.DiffDays(y.StartDate, checkIn) > 0) ? (DbFunctions.DiffDays(y.EndDate, checkIn) <= 0 ? false : true) : true
))
.ToList();
}
which works perfectly fine but it does not eagerly load, when i convert result of above query to my business model it again fires some queries on db.HotelRoomBlackoutPeriods
Please provide the optimum way to achieve this.
thanks in advance.
sorry (if i am asking something nonsense ) in advance

Eager loading does not work when you change the shape of the result set. Shape of your result set is HotelRoom type. Once you use any manual join or projection, you are changing the result set and eager loading request is ignored.

Related

Linq statement Where within a Where

I am building a query tool for use by non technical staff to retrieve records from the database.
I have a form with various drop downs which can be selected by the user depending on what they are looking for.
I have come across a problem where my query is returning records that do not match the users selection.
I believe this is only happening when I am querying the joined tables.
I have the following:
results = results.Where(c => c.CustomerEnrollment
.Where(x => x.CustomerCategoryID == CustomerCategoryID)
.Any());
results = results.Where(c => c.CustomerEnrollment
.Where(x => x.StartDate <= DateRangeStart && x.EndDate >= DateRangeStart)
.Any());
This will return results for the correct category but not within the specified date range.
I have also tried:
results = results.Where(c => c.CustomerEnrollment
.Any(x => x.CustomerCategoryID == CustomerCategoryID));
Try changing your date range check as;
Change:
x => x.StartDate <= DateRangeStart && x.EndDate >= DateRangeStart
To:
//StartDate should be greater than or equal to
//EndDate should be less than or equal to
//Also you are using same variable DateRangeStart to check start and end
x => x.StartDate >= DateRangeStart && x.EndDate <= DateRangeEnd
Your query returns categories that have any CustomerEnrollment having their Id, and also have any CustomerEnrollment in the the required data range, but these CustomerEnrollments are not necessarily the same.
To make sure that you get categories with CustomerEnrollments that fulfill both conditions you have to do:
results = results.Where(c => c.CustomerEnrollment
.Where(x => x.CustomerCategoryID == CustomerCategoryID
&& x.StartDate <= DateRangeStart
&& x.EndDate >= DateRangeStart)
.Any());
With PredicateBuilder you can parametrize the conditions:
using LinqKit;
...
var pred = Predicate.True<CustomerEnrollment>();
if (CustomerCategoryID > 0)
pred = pred.And(c => c.CustomerCategoryID == CustomerCategoryID);
if (DateRangeStart.HasValue)
pred = pred.And(c => c.StartDate <= DateRangeStart
&& c.EndDate >= DateRangeStart);
results = results.AsExpandable()
.Where(c => c.CustomerEnrollment.AsQueryable()
.Any(pred));
The combination of .AsExpandable() and .AsQueryable() appears to be the only way to avoid exceptions.

linq to sql fetching all the records category wise in the list<> and then looping

i am fetching all the records from the database with the help of this query organization wise. they become about 30-40 records
List<PagesRef> paages = (from pagess in pagerepository.GetAllPages()
join pagesref in pagerepository.GetAllPageRef()
on pagess.int_PageId equals pagesref.int_PageId
where (pagess.int_PostStatusId != 3 && pagess.int_OrganizationId == Authorization.OrganizationID)
&& pagesref.int_PageRefId == pagesref.Pages.PagesRefs.FirstOrDefault(m => m.int_PageId == pagess.int_PageId && m.bit_Active == true && (m.vcr_PageTitle != null && m.vcr_PageTitle != "")).int_PageRefId
select pagesref).ToList();
next the next step what i want to do is to loop through the above list as linq to object query without going to the database to generate 3 level hierarchical record. can some one give me some insight or idea how can i do it?
edit
var parentrecord = paages.Where(n => n.Pages.int_PageParent == 0).OrderBy(m => m.Pages.int_SortOrder == null).OrderBy(m => m.int_PageId);
foreach (var secondlevel in parentrecord) // if parentrecord found
{
var seclevel = paages.Where(m => m.Pages.int_PageParent == secondlevel.Pages.int_PageId).OrderBy(m => m.Pages.int_SortOrder == null).OrderBy(m => m.Pages.int_SortOrder);
secondlevel.vcr_PageTitle = "parent";
pagesreff.Add(secondlevel); // if parentrecord found then loop and add in there
foreach (var thdlevel in seclevel)
{
var thirdlevel = paages.Where(m => m.Pages.int_PageParent == thdlevel.Pages.int_PageId).OrderBy(m => m.Pages.int_SortOrder == null).OrderBy(m => m.int_PageId).OrderBy(m => m.Pages.int_SortOrder);
thdlevel.vcr_PageTitle = "child";
pagesreff.Add(thdlevel); // if parentrecord child found then loop and add in there
foreach (var thd in thirdlevel)
{
thd.vcr_PageTitle = "subchild";
pagesreff.Add(thd); // if parentrecord child found then loop and add in there
}
}
}
After ToList(); linq-to-sql go to database and get rows. After that, you have collection of objects and can do what you want with linq to objects:
var filteredList = paages.Where(someFilter);
there will be no new sql requests.
Update
Your problem is that you filter in navigation property, so you should load your navigation property with your first query. I'm not sure (linq-to-sql was many years ago:)), but this should help you (I assume that m.Pages is of type Page):
List<PagesRef> paages = (from pagess in pagerepository.GetAllPages()
join pagesref in pagerepository.GetAllPageRef()
on pagess.int_PageId equals pagesref.int_PageId
where (pagess.int_PostStatusId != 3 && pagess.int_OrganizationId == Authorization.OrganizationID)
&& pagesref.int_PageRefId == pagesref.Pages.PagesRefs.FirstOrDefault(m => m.int_PageId == pagess.int_PageId && m.bit_Active == true && (m.vcr_PageTitle != null && m.vcr_PageTitle != "")).int_PageRefId
select pagesref).AssociateWith<Page>.ToList();

Entity Framework/ Linq - groupby and having clause

Given the query below
public TrainingListViewModel(List<int> employeeIdList)
{
this.EmployeeOtherLeaveItemList =
CacheObjects.AllEmployeeOtherLeaves
.Where(x => x.OtherLeaveDate >= Utility.GetToday() &&
x.CancelDate.HasValue == false &&
x.OtherLeaveId == Constants.TrainingId)
.OrderBy(x => x.OtherLeaveDate)
.Select(x => new EmployeeOtherLeaveItem
{
EmployeeOtherLeave = x,
SelectedFlag = false
}).ToList();
}
I want to put in the employeeIdList into the query.
I want to retrieve all of the x.OtherLeaveDate values where the same x.OtherLeaveDate exists for each join where x.EmployeeId = (int employeeId in employeeIdList)
For example if there are EmployeeIds 1, 2, 3 in employeeIdList and in the CacheObjects.AllEmployeeOtherLeaves collection there is a date 1/1/2001 for all 3 employees, then retreive that date.
If I read you well it should be something like
var grp = this.EmployeeOtherLeaveItemList =
CacheObjects.AllEmployeeOtherLeaves
.Where(x => x.OtherLeaveDate >= Utility.GetToday()
&& x.CancelDate.HasValue == false
&& x.OtherLeaveId == Constants.TrainingId
&& employeeIdList.Contains(x.EmployeeId)) // courtesy #IronMan84
.GroupBy(x => x.OtherLeaveDate);
if (grp.Count() == 1)
{
var result = g.First().Select(x => new EmployeeOtherLeaveItem
{
EmployeeOtherLeave = x,
SelectedFlag = false
})
}
First the data is grouped by OtherLeaveDate. If the grouping results in exactly one group, the first (and only) IGrouping instance is taken (which is a list of Leave objects) and its content is projected to EmployeeOtherLeaveItems.
To the where statement add "&& employeeIdList.Contains(x.EmployeeId)"
I need to thank #IronMan84 and #GertArnold for helping me along, and I will have to admonish myself for not being clearer in the question. This is the answer I came up with. No doubt it can be improved but given no one has responded to say why I will now tick this answer.
var numberOfEmployees = employeeIdList.Count;
var grp = CacheObjects.AllEmployeeOtherLeaves.Where(
x =>
x.OtherLeaveDate >= Utility.GetToday()
&& x.CancelDate.HasValue == false
&& x.OtherLeaveId == Constants.TrainingId
&& employeeIdList.Contains(x.EmployeeId))
.GroupBy(x => x.OtherLeaveDate)
.Select(x => new { NumberOf = x.Count(), Item = x });
var list =
grp.Where(item => item.NumberOf == numberOfEmployees).Select(item => item.Item.Key).ToList();

Why am I geting this error: Operator ā€˜&&ā€™ cannot be applied to operands of type System.Linq.IQueryable?

Iā€™m trying to retrieve data from an entity and populate a viewModel property like this:
viewModel.Enrollments = db.Enrollments.Where(b => b.classDays == "Monday") && (db.Enrollments.Where(b => b.CourseID == courseID);
but I get a operator && cannot be applied to operands of type System.Linq.IQerable<> error. Can you help with a way to find all Monday class with the same ID?
I tried this: viewModel.Enrollments = db.Enrollments.Where(b => b.classDays == "Monday") but I get all Mondays courses but I want to limit them to a specific courseID.
Please help!
You need to examine your parentheses. This code won't even compile:
viewModel.Enrollments = db.Enrollments.Where(b => b.classDays == "Monday")
&& (db.Enrollments.Where(b => b.CourseID == courseID);
In that code you're trying to use && between two calls to .Where(), which return an IQueryable. You probably mean to use && within the .Where() clause:
viewModel.Enrollments = db.Enrollments.Where(b => (b.classDays == "Monday")
&& (b.CourseID == courseID));
Or perhaps append a second .Where() clause:
viewModel.Enrollments = db.Enrollments.Where(b => b.classDays == "Monday")
.Where(b => b.CourseID == courseID);
Note that .Where() can be chained indefinitely, essentially resulting in applying each clause in turn in an AND fashion in the resulting query.
&& Operator should be used with conditions inside where NOT with sets of enrollments (in your case)
Try This:
viewModel.Enrollments = db.Enrollments.Where(b => (b.classDays == "Monday") && (b.CourseID == courseID));

Linq to Entities performance problem with many columns

I am having an issue with getting linq to entities to perform well. The query I have (not mine, maintaining someone's code :-)), has several includes that I've determined are all necessary for the WPF screen that consumes the results of this query.
Now, the SQL generated executes very fast and only returns one row of data. But it is returning 570 columns, and i think the performance hit is in the overhead of creating all the objects and all of those fields.
I've tried using lazy loading, but that doesn't seem to have any effect on performance.
I've tried removing any of the "include" statements that aren't necessary, but it appears that they all are needed.
here's the linq query:
var myQuery =
from appt in ctx.Appointments
.Include("ScheduleColumnProfile")
.Include("EncounterReason")
.Include("Visit")
.Include("Visit.Patient")
.Include("Visit.Patient.PatientInsurances")
.Include("Visit.Patient.PatientInsurances.InsuranceType")
.Include("Visit.Patient.PatientInsurances.InsuranceCarrier")
.Include("MasterLookup")
.Include("User1")
.Include("User2")
.Include("Site")
.Include("Visit.Patient_CoPay")
.Include("Visit.Patient_CoPay.User")
.Include("Visit.VisitInstructions.InstructionSheet")
where appt.VisitId == visitId
&& appt.MasterLookup.LookupDescription.ToUpper() != Rescheduled
&& appt.Site.PracticeID == practiceId
&& appt.MasterLookup.LookupDescription.ToUpper() != Cancelled
orderby appt.AppointmentId descending
select appt;
The SQL generate is 4000 lines long with 570 columns in the select statment and 3 or 4 Union ALLs, so I'm not going to paste it here unless someone REALLY wants to see it. Basically, i'm looking for a way to get rid of the unions if possible, and trim down the columns to only what's needed.
Help!
:-)
if anyone is keeping track, this is the solution that ended up working for me. Thanks to everyone who commented and made suggestions... it eventually lead me to what i have below.
ctx.ContextOptions.LazyLoadingEnabled = true;
var myQuery =
from appt in ctx.Appointments
where appt.VisitId == visitId
&& appt.MasterLookup.LookupDescription.ToUpper() != Rescheduled
&& appt.Site.PracticeID == practiceId
&& appt.MasterLookup.LookupDescription.ToUpper() != Cancelled
orderby appt.AppointmentId descending
select appt;
var myAppt = myQuery.FirstOrDefault();
ctx.LoadProperty(myAppt, a => a.EncounterReason);
ctx.LoadProperty(myAppt, a => a.ScheduleColumnProfile);
ctx.LoadProperty(myAppt, a => a.Visit);
ctx.LoadProperty(myAppt, a => a.MasterLookup);
ctx.LoadProperty(myAppt, a => a.User1);
ctx.LoadProperty(myAppt, a => a.User2);
ctx.LoadProperty(myAppt, a => a.PatientReferredProvider);
var myVisit = myAppt.Visit;
ctx.LoadProperty(myVisit, v => v.Patient);
ctx.LoadProperty(myVisit, v => v.Patient_CoPay);
ctx.LoadProperty(myVisit, v => v.VisitInstructions);
ctx.LoadProperty(myVisit, v => v.EligibilityChecks);
var pat = myVisit.Patient;
ctx.LoadProperty(pat, p => p.PatientInsurances);
//load child insurances
foreach (PatientInsurance patIns in myAppt.Visit.Patient.PatientInsurances)
{
ctx.LoadProperty(patIns, p => p.InsuranceType);
ctx.LoadProperty(patIns, p => p.InsuranceCarrier);
}
//load child instruction sheets
foreach (VisitInstruction vi in myAppt.Visit.VisitInstructions)
{
ctx.LoadProperty(vi, i => i.InstructionSheet);
}
//load child copays
foreach (Patient_CoPay coPay in myAppt.Visit.Patient_CoPay)
{
ctx.LoadProperty(coPay, c => c.User);
}
//load child eligibility checks
foreach (EligibilityCheck ec in myAppt.Visit.EligibilityChecks)
{
ctx.LoadProperty(ec, e => ec.MasterLookup);
ctx.LoadProperty(ec, e => ec.EligibilityResponse);
}
I would recommend creating a new Class that contains only the properties that you need to display. When you project to a new type you don't need to have Include statements, but you can still access the navigation properties of the entity.
var myQuery = from appt in ctx.Appointments
where appt.VisitId == visitId
&& appt.MasterLookup.LookupDescription.ToUpper() != Rescheduled
&& appt.Site.PracticeID == practiceId
&& appt.MasterLookup.LookupDescription.ToUpper() != Cancelled
orderby appt.AppointmentId descending
select new DisplayClass
{
Property1 = appt.Prop1,
Proeprty2 = appt.Visit.Prop1,
.
.
.
};

Resources