How to combine a multi-part LINQ to Entities query - c#

I posted this code snippet over at Stack Exchange's Code Review (beta) in order to obtain some feedback on how best to refactor a multi-part LINQ query.
Being relatively new to LINQ, I'm not really sure where to begin with this query.
If anybody could give me any advice on combining a few of the LINQ queries within the method, I'd appreciate it; especially the 'exclude' IQueryable collections into the main query (see comments).
The query is not particularly performant at the moment and any advice you could give in order to improve its performance from a code perspective would be appreciated.
The comments received on Code Review were more architectural, however I'm not currently in a position where I can move anything to the database at this time.
I appreciate it's a large method, but I've posted the whole lot to give context.
Thanks in advance for any advice you're able to give.
The Method
/// Get templates by username and company
public List<BrowsingSessionItemModel> GetItemBrowsingSessionItems(
int companyId,
string userName,
Boolean hidePendingDeletions,
Boolean hideWithAppointmentsPending,
Boolean hideWithCallBacksPending,
int viewMode,
string searchString,
List<int?> requiredStatuses,
List<int?> requiredSources,
string OrderBy,
BrowsingSessionLeadCustomField fieldFilter)
{
try
{
IQueryable<Lead> exclude1;
IQueryable<Lead> exclude2;
IQueryable<Lead> exclude3;
//To prepare call backs pending
if (hideWithCallBacksPending == true)
{
exclude1 = (from l1 in db.Leads
where (l1.Company_ID == companyId)
from l2 // Hiding Pending Call Backs
in db.Tasks
.Where(o => (o.IsCompleted ?? false == false)
&& (o.TaskType_ID == (int)RecordEnums.TaskType.PhoneCall)
&& (o.Type_ID == (int)RecordEnums.RecordType.Lead)
&& (o.Item_ID == l1.Lead_ID)
&& (o.Due_Date > EntityFunctions.AddDays(DateTime.Now, -1))
)
select l1);
}
else
{
exclude1 = (from l1 in db.Leads
where (0 == 1)
select l1);
}
//To prepare appointments backs pending
if (hideWithAppointmentsPending == true)
{
exclude2 = (from a1 in db.Leads
where (a1.Company_ID == companyId)
from a2 // Hiding Pending Appointments
in db.Tasks
.Where(o => (o.IsCompleted ?? false == false)
&& (o.TaskType_ID == (int)RecordEnums.TaskType.Appointment)
&& (o.Type_ID == (int)RecordEnums.RecordType.Lead)
&& (o.Item_ID == a1.Lead_ID)
&& (o.Due_Date > EntityFunctions.AddDays(DateTime.Now, -1))
)
select a1);
}
else
{
exclude2 = (from a1 in db.Leads
where (0 == 1)
select a1);
}
//To prepare deletions
if (hidePendingDeletions == true)
{
exclude3 = (from d1 in db.Leads
where (d1.Company_ID == companyId)
from d2 // Hiding Pending Deletions
in db.LeadDeletions
.Where(o => (o.LeadId == d1.Lead_ID))
select d1);
}
else
{
exclude3 = (from d1 in db.Leads
where (0 == 1)
select d1);
}
// MAIN QUERY <--
IQueryable<Lead> list = (from t1 in db.Leads
from t2
in db.LeadSubOwners
.Where(o => t1.Lead_ID == o.LeadId && o.Expiry >= DateTime.Now)
.DefaultIfEmpty()
where (t1.Company_ID == companyId)
where ((t2.Username == userName) && (viewMode == 1)) || ((t1.Owner == userName) && (viewMode == 1)) || ((viewMode == 2)) // Either owned by the user or mode 2 (view all)
select t1).Except(exclude1).Except(exclude2).Except(exclude3);
// Filter sources and statuses seperately
if (requiredStatuses.Count > 0)
{
list = (from t1 in list
where (requiredStatuses.Contains(t1.LeadStatus_ID))
select t1);
}
if (requiredSources.Count > 0)
{
list = (from t1 in list
where (requiredSources.Contains(t1.LeadSource_ID))
select t1);
}
// Do custom field filter here
if (fieldFilter != null)
{
string stringIntegerValue = Convert.ToString(fieldFilter.IntegerValue);
switch (fieldFilter.FieldTypeId)
{
case 1:
list = (from t1 in list
from t2
in db.CompanyLeadCustomFieldValues
.Where(o => t1.Lead_ID == o.Lead_ID && fieldFilter.TextValue == o.LeadCustomFieldValue_Value)
select t1);
break;
case 2:
list = (from t1 in list
from t2
in db.CompanyLeadCustomFieldValues
.Where(o => t1.Lead_ID == o.Lead_ID && stringIntegerValue == o.LeadCustomFieldValue_Value)
select t1);
break;
default:
break;
}
}
List<Lead> itemsSorted; // Sort here
if (!String.IsNullOrEmpty(OrderBy))
{
itemsSorted = list.OrderBy(OrderBy).ToList();
}
else
{
itemsSorted = list.ToList();
}
var items = itemsSorted.Select((x, index) => new BrowsingSessionItemModel
{
Id = x.Lead_ID,
Index = index + 1
});
return items.ToList();
}
catch (Exception ex)
{
logger.Info(ex.Message.ToString());
return new List<BrowsingSessionItemModel>();
}
}

I don't understand why this:
false == false
And that:
where (0 == 1)
Then for excludes 1 and 2:
//To prepare call backs pending
var phoneCallTypeId = (int) RecordEnums.TaskType.PhoneCall;
var exclude1 = GetExclude(hideWithCallBacksPending, companyId, phoneCallTypeId);
//To prepare appointments backs pending
var appointmentTypeId = (int) RecordEnums.TaskType.Appointment;
var exclude2 = GetExclude(hideWithCallBacksPending, companyId, appointmentTypeId);
using the following GetExclude method:
private object GetExclude(bool hideWithCallBacksPending, int companyId, int typeId)
{
return hideWithCallBacksPending
? (from l1 in db.Leads
where (l1.Company_ID == companyId)
from l2
// Hiding Pending Call Backs
in
db.Tasks.Where(
o =>
(o.IsCompleted ?? false) &&
(o.TaskType_ID == typeId) &&
(o.Type_ID == (int) RecordEnums.RecordType.Lead) &&
(o.Item_ID == l1.Lead_ID) &&
(o.Due_Date > EntityFunctions.AddDays(DateTime.Now, -1)))
select l1)
: (from l1 in db.Leads where (0 == 1) select l1);
}
Exclude3:
//To prepare deletions
var exclude3 = hidePendingDeletions
? (from d1 in db.Leads
where (d1.Company_ID == companyId)
from d2
// Hiding Pending Deletions
in db.LeadDeletions.Where(o => (o.LeadId == d1.Lead_ID))
select d1)
: (from d1 in db.Leads where (0 == 1) select d1);
Excludes 1 and 2 can be called inline as they are short:
// Either owned by the user or mode 2 (view all)
...select t1)
.Except(GetExclude(hideWithCallBacksPending, companyId, phoneCallTypeId))
.Except(GetExclude(hideWithCallBacksPending, companyId, appointmentTypeId))
.Except(exclude3);

Related

Refactor and reduce cyclomatic complexity with LINQ

I have a method that I feel like could be refactored more efficiently with LINQ.
The purpose of the function is to use some logic to determine which phone number to return. The logic is: Any returned number must be sms_capable. If a number was last used for an rx, use it, otherwise return the first valid number by type in this order: Other, Home, Office
string GetDefaultSMSPhoneNumber(IEnumerable<PhoneNumbers> patientNumbers)
{
const int PHONE_TYPE_HOME = 1;
const int PHONE_TYPE_OFFICE = 3;
const int PHONE_TYPE_OTHER = 9;
var phoneNumberByType = patientNumbers.Where(p => p.sms_capable == 1).GroupBy(p => p.phone_type_id);
// Select the phone number last used in creating a prescription
if (patientNumbers.Where(p => p.sms_capable == 1 && p.last_used_for_rx == 1).Count() > 0)
{
return patientNumbers.Where(p => p.sms_capable == 1 && p.last_used_for_rx == 1).FirstOrDefault().phone_number;
}
// If no number has been used, select a configured SMS number in the following order (Other, Home, Office)
if (patientNumbers.Where(p => p.sms_capable == 1 && p.phone_type_id == PHONE_TYPE_OTHER).Count() > 0)
{
return patientNumbers.Where(p => p.sms_capable == 1 && p.phone_type_id == PHONE_TYPE_OTHER).FirstOrDefault().phone_number;
}
// If no number has been used, select a configured SMS number in the following order (Other, Home, Office)
if (patientNumbers.Where(p => p.sms_capable == 1 && p.phone_type_id == PHONE_TYPE_HOME).Count() > 0)
{
return patientNumbers.Where(p => p.sms_capable == 1 && p.phone_type_id == PHONE_TYPE_HOME).FirstOrDefault().phone_number;
}
// If no number has been used, select a configured SMS number in the following order (Other, Home, Office)
if (patientNumbers.Where(p => p.sms_capable == 1 && p.phone_type_id == PHONE_TYPE_OFFICE).Count() > 0)
{
return patientNumbers.Where(p => p.sms_capable == 1 && p.phone_type_id == PHONE_TYPE_OFFICE).FirstOrDefault().phone_number;
}
return string.Empty;
}
I know the first thing I can do is filter the list to only sms_capable numbers. I feel like I should be able to use .GroupBy to group the numbers by there type, but after they're grouped I'm not sure how to return the first non empty value? I feel like I'm looking for a way to coalesce in linq?
string GetDefaultSMSPhoneNumber(IEnumerable<PhoneNumbers> patientNumbers)
{
const int PHONE_TYPE_HOME = 1;
const int PHONE_TYPE_OFFICE = 3;
const int PHONE_TYPE_OTHER = 9;
var phoneNumberByType = patientNumbers.Where(p => p.sms_capable == 1).GroupBy(p => p.phone_type_id);
var phoneNumber = patientNumbers.FirstOrDefault(p => p.sms_capable == 1 && p.last_used_for_rx == 1)?.phone_number;
// Doesn't work
if (string.IsNullOrEmpty(phoneNumber))
{
var number = phoneNumberByType.FirstOrDefault(p => p.Key == PHONE_TYPE_OTHER && p.Where(x => !string.IsNullOrEmpty(x.phone_number)) ||
(p.Key == PHONE_TYPE_HOME && p.Where(x => !string.IsNullOrEmpty(x.phone_number)) ||
(p.Key == PHONE_TYPE_OFFICE && p.Where(x => !string.IsNullOrEmpty(x.phone_number))));
}
If you need matching against predicates in specific order you can create a collection of Func<PhoneNumbers, bool> and iterate it (also if PhoneNumbers is a class or record then you don't need Count, if it is not, better use Any instead of count):
string GetDefaultSMSPhoneNumber(IEnumerable<PhoneNumbers> patientNumbers)
{
const int PHONE_TYPE_HOME = 1;
const int PHONE_TYPE_OFFICE = 3;
const int PHONE_TYPE_OTHER = 9;
var predicates = new List<Func<PhoneNumbers, bool>>()
{
p => p.sms_capable == 1 && p.last_used_for_rx == 1,
p => p.sms_capable == 1 && p.phone_type_id == PHONE_TYPE_OTHER,
p => p.sms_capable == 1 && p.phone_type_id == PHONE_TYPE_HOME,
p => p.sms_capable == 1 && p.phone_type_id == PHONE_TYPE_OFFICE
}; // Can be moved to static field
// prevent potential multiple materialization of the source
var enumerated = patientNumbers as ICollection<PhoneNumbers> ?? patientNumbers.ToArray();
foreach (var predicate in predicates)
{
var firstOrDefault = enumerated.FirstOrDefault(predicate);
if (firstOrDefault is not null)
{
return firstOrDefault.phone_number;
}
}
return string.Empty;
}
Also in this particular case you can "prefilter" the enumerated with .Where(p => p.sms_capable == 1) to improve performance a bit:
// ...
var enumerated = patientNumbers
.Where(p => p.sms_capable == 1)
.ToArray();
var predicates = new List<Func<PhoneNumbers, bool>>()
{
p => p.last_used_for_rx == 1,
p => p.phone_type_id == PHONE_TYPE_OTHER,
p => p.phone_type_id == PHONE_TYPE_HOME,
p => p.phone_type_id == PHONE_TYPE_OFFICE
};
// ...
This isnt using linq, but you can refactor this by putting some of the complexity into their own methods
private IEnumerable<IGrouping<int, PhoneNumbers>> GetSmsCapablePhoneNumbersByType(IEnumerable<PhoneNumbers> patientNumbers)
{
return patientNumbers.Where(p => p.sms_capable == 1).GroupBy(p => p.phone_type_id);
}
private PhoneNumbers GetLastUsedSmsNumber(IEnumerable<PhoneNumbers> patientNumbers)
{
return patientNumbers.FirstOrDefault(p => p.sms_capable == 1 && p.last_used_for_rx == 1);
}
private PhoneNumbers GetFirstSmsNumberByType(IEnumerable<PhoneNumbers> patientNumbers, int phoneTypeId)
{
return patientNumbers.FirstOrDefault(p => p.sms_capable == 1 && p.phone_type_id == phoneTypeId);
}
public string GetDefaultSMSPhoneNumber(IEnumerable<PhoneNumbers> patientNumbers)
{
var phoneNumberByType = GetSmsCapablePhoneNumbersByType(patientNumbers);
var lastUsedSmsNumber = GetLastUsedSmsNumber(patientNumbers);
if (lastUsedSmsNumber != null)
{
return lastUsedSmsNumber.phone_number;
}
var defaultSmsNumber = GetFirstSmsNumberByType(patientNumbers, PHONE_TYPE_OTHER)
?? GetFirstSmsNumberByType(patientNumbers, PHONE_TYPE_HOME)
?? GetFirstSmsNumberByType(patientNumbers, PHONE_TYPE_OFFICE);
if (defaultSmsNumber != null)
{
return defaultSmsNumber.phone_number;
}
return string.Empty;
}
If you do it correctly, your method names should describe exactly whats happening, so when somone else reads your code they should be able to follow whats happening by reading the method names (This also means there is less need for comments)

Remove a range of items from a list without looping

Hi Is there a more ellegant way of doing this must I do the loop is there like a range funciton I could just remove all the items found
Sorry I should have showing how my qry is being inserted.
Btw these are two different entities that I am removing from I hope you get the idea.
var qry = db.AssemblyListItems.AsNoTracking().Where(x =>
x.ProductionPlanID == (long)_currentPlan.ProductionPlan ).ToList();
var hasbeenAssembled = db.CompletedPrinteds.AsNoTracking().Where(x =>
x.ProductionPlanId == item.ProductionPlanID).ToList();
var hasbeenFound = db.CompletedPrinteds.AsNoTracking().Where(x =>
x.ProductionPlanId== item.ProductionPlanID).ToList();
foreach (var subitem in hasbeenAssembled )
{
if(item.ProductionPlanID ==subitem.ProductionPlanId && item.DocumentNo == subitem.DocumentNo && item.DocumentNo == subitem.DocumentNo && item.OutstandingToMake ==0)
{
qry.RemoveAll(x => x.ProductionPlanID == subitem.ProductionPlanId && x.DocumentNo == item.DocumentNo && x.ItemCode == subitem.StockCode && item.OutstandingToMake ==0);
}
}
public List<AssemblyListItems> RemoveDespatchedItems(List<AssemblyListItems> AssemblyItems)
{
foreach (AssemblyListItems item in AssemblyItems)
{
using (var db = new LiveEntities())
{
var hasNotBeenDespatched = db.PalletizedItems.Where(w => w.Despatched != "Not Despatched");
foreach (var subitem in hasNotBeenDespatched)
{
AssemblyItems.RemoveAll(x => x.ProductionPlanID == subitem.ProductionPlanID && x.DocumentNo == item.DocumentNo && x.ItemCode == subitem.StockCode);
}
}
}
return AssemblyItems;
}
I just need to remove the items from the first query hasNotBeenDespatched from the second query.As could be over 400 items i want it to be efficient as possible.
Edit 2
I am a we bit closer thanks buts its still not removing the items from the removedespatchitems from the assebmittems I do not no why
public List<AssemblyListItems> RemoveDespatchedItems(List<AssemblyListItems> AssemblyItems, Int64 ProductionPlanId)
{
using (var db = newLiveEntities())
{
List<PalletizedItems> removeDespatchItems = db.PalletizedItems.Where(w => w.Despatched != "Not Despatched" && w.ProductionPlanID == ProductionPlanId).ToList();
var itemsDocumentNo = db.PalletizedItems.Select(x => x.ProductionPlanItemID).ToList();
foreach (var subitem in removeDespatchItems) {
AssemblyItems.RemoveAll(x => x.ProductionPlanID == subitem.ProductionPlanID && itemsDocumentNo.Contains(x.ProductionPlanItemID) && x.ItemCode == subitem.StockCode && x.LineQuantity==x.AllocatedQuantity);
}
}
return AssemblyItems;
}
Not 100% I get exactly how it should be.
However in general you could use join that would result in it being done in the database. Something like this:
var remainingItems = (from ali in db.FUEL_AssemblyListItems
join completed in db.FuelCompletedPrinteds
on new { ali.ProductionPlanID, ali.DocumentNo, ali.ItemCode } equals new { completed.ProductionPlanID, completed.DocumentNo, completed.StockCode }
join dispatched in db.FUEL_PalletizedItems
on new { ali.ProductionPlanID, ali.DocumentNo, ali.ItemCode } equals new { dispatched.ProductionPlanID, dispatched.DocumentNo, dispatched.StockCode }
where (ali.ProductionPlanID == (long) _currentPlan.ProductionPlan
&& ali.DocumentNo == completed.DocumentNo
&& completed.OutstandingToMake == 0
&& dispatched.Despatched != "Not Despatched")
select ali).ToList();
Depending upon the records in the database the join might need to be a outer join which needs a slightly different syntax but hopefully you've got a starting point.

Extending a correlated Subquery to include another query in LINQ

I am trying to extend a correlated subquery to include another query in LINQ.
In SQL, this would be the query.
SELECT V.Car_ID
FROM VEHICLES V, AGREEMENTS A, AGREEMENTS A1
WHERE A.Car_ID = V.Car_ID
AND enDate >= A.Hire_Start_Date
AND stDate <= A.Hire_End_Date
AND A1.CAR_ID = V.CAR_ID
AND location = A1.Return_Location
AND A1.Return_date =
(SELECT MAX(A2.Return_Date)
FROM AGREEMENT A2
WHERE A2.VEHICLE_ID = V.VEHICLE_ID
AND A2.Return_Date < stDate)**
With some help, I have got as far as querying if the vehicle has an agreement but I can't seem to extend it to check that the AGREEMENT return location matches the search location. This is my code so far.
var cars = from v in db.VEHICLEs
where !db.AGREEMENTs.Any(a => (a.CAR_ID == v.CAR_ID
&& a.STATUS_OPEN == true
&& enDate >= a.HIRE_START_DATE
&& strtDate <= a.HIRE_END_DATE))
select v;
The following part of the sql query is missing from my working LINQ query.
AND A1.CAR_ID = V.CAR_ID
AND location = A1.Return_Location
AND A1.Return_date =
(SELECT MAX(A2.Return_Date)
FROM AGREEMENT A2
WHERE A2.VEHICLE_ID = V.VEHICLE_ID
AND A2.Return_Date < stDate)**
Any help would be greatly appreciated.
Your Sql query is an inner join, so in Linq you can do:
var cars = from v in db.VEHICLEs
from a in db.AGREEMENTs
from a1 in db.AGREEMENTs
where a.CAR_ID == v.CAR_ID
&& enDate >= a.HIRE_START_DATE
&& strtDate <= a.HIRE_END_DATE
&& a1.CAR_ID == v.CAR_ID
&& location == a1.RETURN_LOCATION
&& a1.RETURN_DATE == db.AGREEMENTs
.Where(a2 => a2.VEHICLE_ID == v.VEHICLE_ID)
.Max(a2 => a2.RETURN_DATE)
select v.CAR_ID;
For selecting additional fields you can do select new { CarId = v.CAR_ID, ... }
Thank you very much derloopkat, you got me there!!! :-)
var cars = from v in db.VEHICLEs
from a1 in db.AGREEMENTs
where !db.AGREEMENTs.Any(a => (a.CAR_ID == v.CAR_ID
&& a.STATUS_OPEN == true
&& enDate >= a.HIRE_START_DATE
&& strtDate <= a.HIRE_END_DATE))
&& a1.CAR_ID == v.CAR_ID
&& schPickUpDepotID == a1.RETURN_LOCATION
&& a1.HIRE_END_DATE == db.AGREEMENTs
.Where(a2 => a2.CAR_ID == v.CAR_ID)
.Max(a2 => a2.HIRE_END_DATE)
select v;

Adding additional linq where clauses based on variables

I'm trying to add additional where clauses to a linq query depending on what variable results are passed to a function.
var allFeedback =
from f in _unitOfWork.Feedback.All()
join b in _unitOfWork.Bookings.All() on f.CourseBookingID equals b.CourseBookingID
join cb in _unitOfWork.CourseBookings.All() on f.CourseBookingID equals cb.CourseBookingID
where b.SiteID == siteID && b.Date >= fromDate && b.Date <= to && b.CancelledID == null
select f;
if (courseID > 0)
{
allFeedback.Where(f => f.CourseBooking.CourseID == courseID);
}
if (facilitatorID == 0)
{
allFeedback.Where(f => f.CourseBooking.FacilitatorID == null);
}
else if (facilitatorID > 0)
{
allFeedback.Where(f => f.CourseBooking.FacilitatorID == facilitatorID);
}
allFeedback.ToList();
I want to add the where clauses to the original query "allFeedback" but when the query is executed the additional clauses are ignored.
Is this possible?
yes its possible just do :
if (courseID > 0)
{
allFeedback = allFeedback.Where(f => f.CourseBooking.CourseID == courseID);
}
if (facilitatorID == 0)
{
allFeedback = allFeedback.Where(f => f.CourseBooking.FacilitatorID == null);
}
else if (facilitatorID > 0)
{
allFeedback = allFeedback.Where(f => f.CourseBooking.FacilitatorID == facilitatorID);
}
You just forgot to assign the result to the variable.

asp.net mvc LINQ datetime problem

string NewsFillter = string.Empty;
List<string> PublishDatePostMeta = (from post in postrepository.GetAllPosts()
join pstmt in postrepository.GetAllPostMetas()
on post.int_PostId equals pstmt.int_PostId
where (post.int_PostTypeId == 4 && post.int_PostStatusId == 2 && post.int_OrganizationId == layoutrep.GetSidebarDetailById(SidebarDetailsId).int_OrganizationId) && pstmt.vcr_MetaKey=="Publish Date"
select pstmt.vcr_MetaValue).ToList();
int DatesCount = PublishDatePostMeta.Count();
foreach (string PublishDate in PublishDatePostMeta)
{
if (PublishDate != "")
{
NewsFillter += System.DateTime.Now + ">=" + Convert.ToDateTime(PublishDate);
}
}
var postsidebar = from post in postrepository.GetAllPosts()
join pstmt in postrepository.GetAllPostMetas()
on post.int_PostId equals pstmt.int_PostId
where (post.int_PostTypeId == 4 && post.int_PostStatusId == 2 && post.int_OrganizationId == layoutrep.GetSidebarDetailById(SidebarDetailsId).int_OrganizationId)
&& (pstmt.vcr_MetaKey.Contains(filter) && pstmt.vcr_MetaValue.Contains("true"))
select post;
1st question .The thing is that how NewsFillter would be accomdated in the postsidebar query in the pstmt object after true ( i would be putting it in contains,equals join or what) .
2nd question . is there any way that a chunk (between &&s) return enumerable and i can get away with this. at this moment it is not allowing that
I haven't udnerstood you properly, but if you want to apply multiple filters, here is my solution:
//Book is a table in the database
List<Expression<Func<Book, bool>>> filters = new List<Expression<Func<Book, bool>>>();
IQueryable<Book> query = dc.Books;
filters.Add(b => b.BookId == long.Parse(id));
//apply all filters
foreach (var f in filters)
query = query.Where(f);
Your questions:
This question requires an example of input and output. Try something like this:
|| pstmt.vcr_MetaKey=="Publish Date" && (
pstmt.vcr_MetaValue == "" || DateTime.Parse(pstmt.vcr_MetaValue) < DateTime.Now)
There is method AsEnumerable, if you mean what I think.

Categories

Resources