Add restriction based on method parameter - nhibernate

I have a method
List<MyType> DoQuery(bool FilterWeek) {
var result = session.QueryOver<MyType>()
.Where (r => r.isValid == 1
&& r.value1 == 2
&& r.name == "XYZ"
&& [...etc, more columns are used...]
)
// how do I go on from this point?
}
if the FilterWeek parameter is true, I want to add an extra "&& r.xyz == 1" clause to the Where criteria. If FilterWeek is false, the query is done.
How do I do that?

if (FilterWeek)
result = result.Where(r => r.xyz ==1);
//...whenever you're done, execute the query using List() or SingleOrDefault()

this:
List<MyType> DoQuery(bool FilterWeek) {
var result = session.QueryOver<MyType>()
.Where (r => r.isValid == 1
&& r.value1 == 2
&& r.name == "XYZ"
&& [...etc, more columns are used...]
);
if(FilterWeek)
result.Where(x => x.Whatever == 1)
//the query won't get executed until here
result.List();
}

Related

GSheets Multi-Select Dropdown affects entire worksheet instead of a specific sheet

I am new to this and I found an old post with this code. I am currently using it but it affects all tabs of my spreadsheet. Is there a way for it to work on a specific tab only? Thank you.
`const separator = ', ';
const dvType = SpreadsheetApp.DataValidationCriteria.VALUE_IN_RANGE;
function onEdit(e) {
if (e.value != null && e.oldValue != null && e.value !== "") {
var dataValidation = e.range.getDataValidation();
if(dataValidation != null && dataValidation.getCriteriaType() == dvType &&
e.value.indexOf(separator) < 0 && e.oldValue.indexOf(e.value) < 0) {
e.range.setValue(e.oldValue + separator + e.value);
}
}
}`
I tried to change the criteria but since I am new to coding, I always get error.

Vue.js - Filter multiple fields

I'm trying to filter a on a nested array inside an array of objects in an Vue.js. Here's a snippet of the component code:
filteredProducts: function() {
if (!this.filters.appointments.length && !this.filters.powers.length && !this.filters.materials.length && !this.filters.lamps.length) return this.products;
return this.products.filter(product => {
return product.filter(p => {
let filteredAppointments = this.filters.appointments ? _.difference(this.filters.appointments, p.appointment.split(',')).length === 0 : true,
filteredPowers = this.filters.powers ? this.filters.powers.includes(p.total_power_lamps) : true,
filteredMaterials = this.filters.materials ? this.filters.materials.includes(p.material) : true,
filteredLamps = this.filters.lamps ? this.filters.lamps.includes(p.count_lamps) : true,
filteredPrice = p.price >= this.rangePrice[0] && p.price <= this.rangePrice[1];
return filteredAppointments && filteredPowers && filteredMaterials && filteredLamps && filteredPrice;
}).length > 0;
});
}
How do I display only the filters that are used ? For example, if only one filter is selected, then no other filters are needed. Also, if several are selected, it should filter by both.
I think this is what you're looking for
filteredProducts () {
let results = this.products
if(this.filters.appointments.length) {
results = results.filter(logicFilterHere)
}
if(this.filters.powers.length) {
results = results.filter(logicFilterHere)
}
if(this.filters.materials.length) {
results = results.filter(logicFilterHere)
}
return results
}

Typescript- filter: Provide multiple conditions in filter dyanmically

I am bit curious if there is any way where i can have multiple conditions dynamically in the filter expression. For example
dataSource.filter(data => data.x === a && data.x === b && data.x === c)
it can have 'n' number of conditions, all will be dynamic only.
Thanks
Not like that, no. You would have to pass in a function that accepts data or data.x and check against every possible condition.
function filterData(possibleValuesOfX: number[] = []) {
return (data: Data) => {
for (let value of possibleValuesOfX) {
if (data.x === value) {
return true;
}
}
return false;
}
}
const filteredDataSource = dataSource.filter(filterData([a, b, c]));

dgrid custom sort with secondary sort column

I'm currently using a custom sort function on a dgrid (pasted below). It doesn't change sorting drastically, just sorts one particular column uniquely and sorts the others case-insensitive. I'd like to add a secondary sort by a column named "scheduled" to be added to the sort when any other column is sorted. I'm just not sure how to go about it. I've seen how to override the sort to sort by two columns, but not when a custom sort is in play. The secondary sort would always be there, not matter what other column is clicked.
For reference I'm running dojo 1.10 and dgrid 1.0. Data is coming from a RequestMemory DStore and I'd really rather this sort happen on the grid rather than back at the store level. Any help would be appreciated.
currGrid.on('dgrid-sort', function (event) {
event.preventDefault();
var sort = event.sort[0];
currGrid.set('sort', function (a, b) {
if (sort.property == "thisField") {
//special sort for thisField
if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
var colorA = a[sort.property].split("|");
var aValue = colorA[0].toLowerCase();
}
if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
var colorB = b[sort.property].split("|");
var bValue = colorB[0].toLowerCase();
}
if (String(aValue) == String(bValue)) {
var result = 0;
} else if (dojo.string.trim(aValue) == "") {
var result = true ? 1 : -1;
} else if (dojo.string.trim(bValue) == "") {
var result = true ? -1 : 1;
} else {
var result = aValue > bValue ? 1 : -1;
}
return result * (sort.descending ? -1 : 1);
} else {
//Sort for all other fields same as always (except toLowerCase)
if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
var aValue = a[sort.property].toLowerCase();
} else {
var aValue = "";
}
if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
var bValue = b[sort.property].toLowerCase();
} else {
var bValue = "";
}
var result = aValue > bValue ? 1 : -1;
return result * (sort.descending ? -1 : 1);
}
});
currGrid.updateSortArrow(event.sort, true);
});
currGrid.startup();
You could do something like below.
currGrid.on('dgrid-sort', function (event) {
event.preventDefault();
var sortSet = [];
sortSet.push(event.sort[0]);
sortSet.push({property: "scheduled"});
currGrid.set('sort', function (a, b) {
var aValue, bValue, result = 0;
for(var i = 0; i < sortSet.length; i++){
var sort = sortSet[i];
if (sort.property == "thisField") {
//special sort for thisField
if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
var colorA = a[sort.property].split("|");
aValue = colorA[0].toLowerCase();
}
if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
var colorB = b[sort.property].split("|");
bValue = colorB[0].toLowerCase();
}
if (String(aValue) == String(bValue)) {
result = 0;
} else if (dojo.string.trim(aValue) == "") {
result = true ? 1 : -1;
} else if (dojo.string.trim(bValue) == "") {
result = true ? -1 : 1;
} else {
result = aValue > bValue ? 1 : -1;
}
return result * (sort.descending ? -1 : 1);
} else {
//Sort for all other fields same as always (except toLowerCase)
if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
aValue = a[sort.property].toLowerCase();
} else {
aValue = "";
}
if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
bValue = b[sort.property].toLowerCase();
} else {
bValue = "";
}
//You need this check here
if(aValue != bValue){
result = aValue > bValue ? 1 : -1;
return result * (sort.descending ? -1 : 1);
}
}
}
return 0;
});
currGrid.updateSortArrow(event.sort, true);
});
currGrid.startup();
I have some concerns about your code, the variables result, aValue and bValue are all local within the if statement and yet they are being used outside the statement. It could result in wrong results if some other variables are defined with the same name in global space. So I have modified them.
So the second section you needed to check if aValue == bValue to return 0.

Complex DateTime query in Entity Framework

I would like to ask how to write a complex DateTime query in Entity Framework as below:
I have this code in service:
Func<DateTime, DateTime> calculate24HoursLater = (date) =>
{
if (date.DayOfWeek == DayOfWeek.Friday)
return date.AddDays(3);
if (date.DayOfWeek == DayOfWeek.Saturday)
return date.AddDays(3).Date;
if (date.DayOfWeek == DayOfWeek.Sunday)
return date.AddDays(2).Date;
return date.AddDays(1);
};
var unactionedEnquiries =
dataContext.ContactedBrokerEnquiries
.Include("ContactedBrokers")
.Where(
x => x.ContactedBrokers.All(c => c.Status == (byte)LeadStatus.Rejected) ||
x.ContactedBrokers.Any(c => c.Status == (byte)LeadStatus.New && calculate24HoursLater(c.CreatedDate) < DateTime.Now)
).OrderByDescending(c => c.CreatedDate);
The result unactionedEnquiries, I expect it should be IQueryable. It means SQL server does not execute until my next statement
However, I get exception on calculate24HoursLater(c.CreatedDate) < DateTime.Now)
This statment cannot translate into SQL statement. I know the reason but I dont know how to write that rule in Entity Framework query
Important: I dont prefer to push all of data into RAM then filter with that condition. Ideally, it should be in SQL-Server
Could you please let me know how to write them in SQL-EF statement?
You may want to take a look at possibly using the SQLFunctions Methods that are available for doing date operations in LINQ queries
Sample (Untested) Try replacing your Func definition with the following:
Func<DateTime, DateTime> calculate24HoursLater = (date) =>
{
if (date.DayOfWeek == DayOfWeek.Friday)
return SqlFunctions.DateAdd("day", 3, date).Value;
if (date.DayOfWeek == DayOfWeek.Saturday)
return SqlFunctions.DateAdd("day", 3, date).Value;
if (date.DayOfWeek == DayOfWeek.Sunday)
return SqlFunctions.DateAdd("day", 2, date).Value;
return SqlFunctions.DateAdd("day", 1, date).Value;
};
As I saw here #andrew's genius answer, you can do:
var calcDate = DateTime.Now.AddHours(-24);
var unactionedEnquiries =
dataContext.ContactedBrokerEnquiries
.Include("ContactedBrokers")
.Where(
x => x.ContactedBrokers.All(c => c.Status == (byte)LeadStatus.Rejected) ||
x.ContactedBrokers.Any(c => c.Status == (byte)LeadStatus.New && c.CreatedDate < calcDate)
).OrderByDescending(c => c.CreatedDate);