I am comparing values in Groovy as follows:
if (creds.username == env.Username && creds.password == env.Password)
{
foundUser = true;
break;
}
It should return foundUser = true , but the issue is the second comparison, ( creds.password == env.Password ) if I take that out it works .
Any ideas as to why this may happen ?
Related
I have a method below pulling specific fields that have null values.
Every record in the table has a status field of 1 or 0. 1 is active and 0 is inactive.
For the query below I only want to pull data for active records i.e status = 1
How can I modify this query to put in the condition for status = 1?
public List<FileModel> GetMyData()
{
var collectedData = _context.MyUploads.AsNoTracking().Where(t =>
t.FirstName == null ||
t.LastName == null ||
t.EmailAddress == null ||
t.Telephone == null)
.Select(x => _mapper.Map<FileModel>(x))
.ToList();
return collectedData;
}
You can chain two Where Linq extension method calls right after one another, like so:
public List<FileModel> GetMyData()
{
var collectedData = _context.MyUploads.AsNoTracking().Where(t =>
t.FirstName == null ||
t.LastName == null ||
t.EmailAddress == null ||
t.Telephone == null)
.Where(t => t.status == 1) // added this line...
.Select(x => _mapper.Map<FileModel>(x))
.ToList();
return collectedData;
}
you should just be able to change the where condition to the below
.Where(t => (t.FirstName == null || t.LastName == null || t.EmailAddress == null || t.Telephone == null) && t.status == 1)
The && means and, so its saying those fields are null and the status equals 1.
I have a large nested if. One portion of the nested if works, however, in the second portion only some of the nested if works. I cannot get the switch case to work.
//Match Scoring //All work, when I flip Poker on top of Match the first and second ifs do not work
if playerMatchCardsArray[0] == playerMatchCardsArray[1] &&
playerMatchCardsArray[1] == playerMatchCardsArray[2]{
} else if playerMatchCardsArray[0] == playerMatchCardsArray[1] ||
playerMatchCardsArray[1] == playerMatchCardsArray[2] ||
playerMatchCardsArray[0] == playerMatchCardsArray[2]{
} else if playerMatchCardsArray[0] != playerMatchCardsArray[1] ||
playerMatchCardsArray[1] != playerMatchCardsArray[2] ||
playerMatchCardsArray[0] != playerMatchCardsArray[2] {
//Poker Scoring //All work, when I flip Poker on top of Match. However the following lines work do not work
// SUITED THREE OF A KIND //does not work
if firstRandomNumber == secondRandomNumber && secondRandomNumber == thirdRandomNumber {
// STRAIGHT FLUSH // works
} else if straightCardsArray[0] == 1 && straightCardsArray[1] == 2 && straightCardsArray[2] == 3 &&
flushFirstRandomNumber == flushSecondRandomNumber &&
flushFirstRandomNumber == flushThirdRandomNumber{
// THREE OF A KIND // does not work
} else if pairFirstRandomNumber == pairSecondRandomNumber &&
pairFirstRandomNumber == pairThirdRandomNumber {
//STRAIGHT FORMULA // works
} else if straightCardsArray[0] == 1 && straightCardsArray[1] == 2 && straightCardsArray[2] == 3 {
// SUITED PAIR // does not work
} else if firstRandomNumber == secondRandomNumber ||
secondRandomNumber == thirdRandomNumber ||
firstRandomNumber == thirdRandomNumber {
// PAIR // does not work
} else if pairFirstRandomNumber == pairSecondRandomNumber ||
pairFirstRandomNumber == pairThirdRandomNumber ||
pairSecondRandomNumber == pairThirdRandomNumber {
// FLUSH // works
} else if flushFirstRandomNumber == flushSecondRandomNumber &&
flushFirstRandomNumber == flushThirdRandomNumber {
Thank you in advance for your time and assistance. It's greatly appreciated!
It was the '}' that was causing the issue.
I don't understand why I'm getting the above mentioned error with following code :
value1 = sessionScope.get("selectedPicture");
if (value1 != "empty"){
var db:NotesDatabase = session.getDatabase(database.getServer(), "product\\picture.nsf");
if (db != null) {
var IDtoChange = toString("7D59468E241AC271C1257D5000417E46") ;
if (IDtoChange = null){
return
}
try {
doc = db.getDocumentByUNID(IDtoChange);
doc.replaceItemValue( 'picWeb', "true" );
doc.save( true, false );
sessionScope.put("selectedUnid","");
} catch(e) {
sessionScope.put("error",(e.toString()));
context.redirectToPage("errorPage.xsp");
}
}
}
When I replace the line doc = db.getDocumentByUNID(IDtoChange); with doc = db.getDocumentByUNID("7D59468E241AC271C1257D5000417E46"); everything works fine
What is wrong ? (in my real code the id isn't hard coded of course)
Should line six be if (IDtoChange == null){ instead of if (IDtoChange = null){. The single = looks to be assigning the value instead of comparing which is ==
I trying to build dynamic filters using findAll on a list. I have a variable that needs to be included in the filter only if not null.
#Test
void testSample(){
def list = [ new Employee(age:22, isManager:false),
new Employee(age:23, isManager:true),
new Employee(age:22, isManager:true) ] as Set
def var = 22;
String query1 = "it.age == var && it.isManager == true "
String query2 = "it.isManager == true"
println list
println list.findAll { var ? query1 : query2 } // Should give 1 record age = 22 and manager
var = null;
println list.findAll { var ? query1 : query2 } // should give 2 records-only manager
}
Both of them giving all the records. Is there anyway I can achieve this in one condition without need to write muiltiple queries ?
Looking some like below (this doesn't work though)
println list.findAll{
if(var) it.age == var &&
it.isManager == true
}
Try with Closures rather than Strings describing what you want to do:
def list = [ new Employee(age:22, isManager:false),
new Employee(age:23, isManager:true),
new Employee(age:22, isManager:true) ] as Set
def var = 22;
Closure query1 = { it.age == var && it.isManager == true }
Closure query2 = { it.isManager == true }
println list
println list.findAll( var ? query1 : query2 ) // Should give 1 record age = 22 and manager
var = null;
println list.findAll( var ? query1 : query2 ) // should give 2 records-only manager
Edit
Do you mean:
println list.findAll{ ( var ? it.age == var : true ) && it.isManager == true }
Or better:
println list.findAll{ ( var != null ? it.age == var : true ) && it.isManager == true }
How can I simply the below if statements?
if ( isset(var1) & isset(var2) ) {
if ( (var1 != something1) || (var2 != something2) ) {
// ... code ...
}
}
Seems like this could be condensed to only one IF statement but am not certain if I'd use an AND or OR
Boolean varsAreSets = isset(var1) & isset(var2); // or some other name that indicates what this is doing
Boolean someMeaningfulName = (var1 != something1) || (var2 != something2); // would suggest a meaningful name but don't know what this is accomplishing
if ( varsAreSets && someMeaningfulName ) {
// ... code ...
}
This makes the code very readable and helps you and whoever reads the code understand what these checks are actually doing.
if (isset(var1) && ((var1 != something1) || (var1 != something2)))
// ... code ...
}
You would use an and because you can only get to the // ... code ... part if both if-statements are true.
You can do:
if(isset(var1) && isset(var2) && ( (var1 != something1) || (var1 != something2) ) ){
//..code
}
As a general example:
if( cond1 && cond2 ) {
if( cond3 || cond4) {
// ...code..
}
}
The code will be executed only when both cond1 and cond2 are true and either of cond3 or cond3 is true.
It's a question of in what order your computer interprets boolean logic:
Take for example the following conditions:
A: False
B: True
if you were to write if (A && B) what your computer actually does is think:
Is A true? No.
Well, A and B can't be true because A isn't true. Therefore this statement is false. [computer ignores the rest of the logic]
Because of this, when you evaluate the statement isset(var1) && ( (var1 != something1) || (var1 != something2) ) it first checks isset(var1) and if that's false, then it skips the rest of the condition, just like your double-if statement.
if ( isset(var1) && isset(var2) && ( (var1 != something1) || (var2 != something2) ) ) {
// ... code ...
}
if (isset(var1) && isset(var2) && ((var1 != something1) || (var2 != something2)))
{
// ... code ...
}
Another option:
if (isset(var1) && isset(var2)
&& !(var1 == something1 && var2 == something2)) {
...
I think most of the examples above that have 1 IF may spit out an error if var1 or var2 is NOT set
(isset($var1) && isset($var2)) ? ($var1!='something1' && $var2!='something2') ? $go=TRUE : $go=FALSE : $go = FALSE;
if ($go){
echo 'hello';
}