where task.RevisionDateTime > startDateTime_
&& task.RevisionDateTime <= endDateTime_
&& task.AutoAuditNotes.ToLower() != auditString.ToLower()
&& (if(isStaging_) condition1 else condition2)
select task).ToList();
Under where clause I have this IF ELSE Statement && (if(isStaging_) condition1 else condition2). How can I Add condition under WHERE clause defends on the value of isStaging_ variable?
Thanks.
You can do something like this :
where task.RevisionDateTime > startDateTime_
&& task.RevisionDateTime <= endDateTime_
&& task.AutoAuditNotes.ToLower() != auditString.ToLower()
&& ((isStaging_ && condition1) || condition2)
select task).ToList();
This isn't a direct answer - I upvoted KiNeTiC's answer. But this comment requires some code formatting that can't go into a comment.
This will become much more readable if you break conditions into separate functions. For example, whatever this is:
&& ((isStaging_ && condition1) || condition2)
Put it in its own function with a name describing what it's checking. The branching will make it difficult for someone to read and follow, even the person who wrote it a week later. But if it's a function call then the name of the function serves as a comment.
Long LINQ queries are oddly satisfying to write, but it can be very difficult for someone else to understand what they're doing.
you try
if(isStaging_)
{
//condition1
where task.RevisionDateTime > startDateTime_
&& task.RevisionDateTime <= endDateTime_
&& task.AutoAuditNotes.ToLower() != auditString.ToLower()
select task).ToList();
}
else
{
//condition2
where task.RevisionDateTime > startDateTime_
&& task.RevisionDateTime <= endDateTime_
&& task.AutoAuditNotes.ToLower() != auditString.ToLower()
select task).ToList();
}
Related
I need to execute an else statement from a if, and another if inside it.
if (!BoxA_IsNull && !BoxB_IsNull && !BoxC_IsNull && !BoxD_IsNull) //Scenario 1
{
if (BoxA == BoxB && BoxC == BoxD) //Scenario 2
{
//Do something
}
}
else
{
// Do something else if
// 1) Scenario 1 hits but not scenario 2
// 2) Scenario 1 does not hit
}
May I know how can I go to else statement regardless if scenario 1 or scenario 2 hits?
Edit: Apologies on scenario confusion. Have edited as above in else statement
At the end I went with the following solution of
if ((!BoxA_IsNull && !BoxB_IsNull && !BoxC_IsNull && !BoxD_IsNull) && (BoxA == BoxB && BoxC == BoxD))
{
//do something
}
Reason is because during if, it will hit 2nd comparison check if 1st null check fails. The purpose of my 1st null check is to prevent null exception during 2nd comparison check.
Store the conditions in boolean variables. Then you can reuse them.
bool nullCondition = !BoxA_IsNull && !BoxB_IsNull && !BoxC_IsNull && !BoxD_IsNull;
bool equalityCondition = BoxA == BoxB && BoxC == BoxD;
if (nullCondition && equalityCondition)
{
// Both conditions are true
}
else
{
// Any of equalitycondition or nullcondition is false
}
You could store the scenarios in two boolean variables, which makes the check more easily:
bool scenario1 = !BoxA_IsNull && !BoxB_IsNull && !BoxC_IsNull && !BoxD_IsNull;
bool scenario2 = BoxA == BoxB && BoxC == BoxD
if (scenario1)
{
if (scenario2)
{
//do something
}
}
if((scenario1 && !scenario2) || !scenario1)
{
//do something else if either Scenario 1 or 2 hits.
}
Depending on what you are doing exactly, you could also give the variables more expressive names.
I'm not sure I'm understanding you question correctly; the logic you seem to want with your example is pretty simple:
if (!BoxA_IsNull && !BoxB_IsNull && !BoxC_IsNull && !BoxD_IsNull)
{
if (BoxA == BoxB && BoxC == BoxD)
{
//Scenario 1 & 2
}
else
{
//Scenario 1 hits but not scenario 2
}
}
else
{
//Scenario 1 does not hit
}
I have Client group logo and Client site logo. And want to assign Client site logo a value based on certain conditions.
This is my code in ClientController.cs :
if (site.ImageURL != null && site.ImageURL != "" && clientGroupImage != "/Content/Images/ClientLogoDefault.svg"
&& site.ImageURL != "/Content/Images/ClientSiteLogoDefault.svg" || site.ImageURL != null
&& site.ImageURL != "" && clientGroupImage == "/Content/Images/ClientLogoDefault.svg"
&& site.ImageURL != "/Content/Images/ClientSiteLogoDefault.svg")
{
model.SiteLogoURL = site.ImageURL;
}
else if (site.ImageURL == "/Content/Images/ClientSiteLogoDefault.svg"
&& clientGroupImage != "/Content/Images/ClientLogoDefault.svg" || site.ImageURL == ""
&& clientGroupImage != "/Content/Images/ClientLogoDefault.svg")
{
model.SiteLogoURL = clientGroupImage;
}
else
{
model.SiteLogoURL = "/Content/Images/ClientSiteLogoDefault.svg";
}
So is there a way to write this code using less "&&" and "||" operators?
There are a few ways you can do this;
By writing boolean function for complex conditions, separate complex conditions from code to improve readability and ease of use. You can also re-use these functions for other similar scenarios.
In your case, extract out string literals into variable or const strings. They will be easier to manage easier to read/use. Like Zaven Suggested.
Nesting conditions when for cases there is common condition. Nesting is similar to if-then lingo.
bool client = clientGroupImage == "/Content/Images/ClientLogoDefault.svg";
bool site = site.ImageURL == "/Content/Images/ClientSiteLogoDefault.svg";
if (!string.IsNullOrEmpty(site.ImageURL) && (!client && !site || client && !site)
{
model.SiteLogoURL = site.ImageURL;
}
else if (!client && (site || site.ImageURL == "")
{
model.SiteLogoURL = clientGroupImage;
}
else
{
model.SiteLogoURL = "/Content/Images/ClientSiteLogoDefault.svg";
}
Your conditional statements are not entirely clear, but in general you could treat compounded conditional statements (with AND and OR) a bit like math operators.
For example, in math you can do this: A*B + A*C = A*(B+C)
Now let treat A,B,C as simple conditions and assume * is AND and + is OR.
So if ((A && B) OR (A && C)) can also be if (A && (B OR C))
For your specific case, I would need some clarifications about the first if statement, but I think you can do something like:
if (!string.IsNullOrEmpty(site.ImageURL) && site.ImageURL != "/Content/Images/ClientSiteLogoDefault.svg" &&
(clientGroupImage != "/Content/Images/ClientLogoDefault.svg" || clientGroupImage == "/Content/Images/ClientLogoDefault.svg"))
Seeing as the only difference is clientGroupImage condition, which if you notice will cover all cases, so you can drop this entirely.
i want to do the following IF statement,
if (checkID.Equals(Convert.ToInt32(txtCheck.Text))
&& drop == 319020000
|| currentFloor[id][0].checkFlag == 1)
what i want to check here it the following thing:
i want to check if this whole statement is true
checkID.Equals(Convert.ToInt32(txtCheck.Text)) && drop == 319020000`
or this statment:
currentFloor[id][0].checkFlag == 1
If 1 of them is true it should go inside the loop.
What am i doing wrong here?
Use parentheses, you have many operators at the same level and precedence may be killing you
if ((checkID.Equals(Convert.ToInt32(txtCheck.Text)) && drop == 319020000)
|| currentFloor[id][0].checkFlag == 1)
http://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx
;)
you have to use additional parentheses like follows,
if ((checkID.Equals(Convert.ToInt32(txtCheck.Text)) && drop == 319020000) || currentFloor[id][0].checkFlag == 1)
Can anyone give me any pointers on how to make this run a bit faster?
return mb_entities.prospects.
FirstOrDefault(x => x.address == person.Add &&
x.homePhone == person.HPhone &&
x.bizPhone == person.BPhone &&
x.cellPhone == person.CPhone &&
x.city == person.City &&
x.state == person.State &&
x.zip == person.Zip &&
x.email == person.Email &&
x.firstName == person.FName &&
x.lastName == person.LName &&
x.middleName == person.MName &&
x.genCode == person.GC) ?? new prospect();
Right now it runs in between 160 and 180 Milliseconds. This would be ok if I didn't have to do it 1000 times.
Any tips would be greatly appreciated. Thanks!
Create an index on the most selective columns (for example, on email, zip and lastname). This will speed it up. It should be one index on multiple columns.
You must have a lot of records in your table that it takes 160ms to execute this once. This is unusually long, even when no index is present.
Let's say you have a List l_mur = new List();
And you populate the list.
Then based on conditions you want to REMOVE some values without requerying...
l_mur.RemoveAt(l_mur.FindIndex(f => (f.xid == tmur.xid && f.sid == tmur.sid && f.mid == tmur.mid && f.bid == tmur.bid)));
However, the code I used here, does not seem to work. It tells me index out of range, but how can it be out of range if I am just searching for something that truly does exist.
List<T>.FindIndex() returns -1 in case there is no match found - which is out of range for List<T>.RemoveAt().
Also note that FindIndex() only returns the index of the first occurrence based on your predicate - if there is more than one match you will only be able to delete the first one of them with your current approach.
A better approach to delete in place based on a predicate would be RemoveAll():
l_mur.RemoveAll(f => (f.xid == tmur.xid && f.sid == tmur.sid && f.mid == tmur.mid && f.bid == tmur.bid));
May be a good idea is to filter the list to a new instance of the list:
var l_mur = l_mur.Where(f => (f.xid != tmur.xid || f.sid != tmur.sid || f.mid != tmur.mid || f.bid != tmur.bid));
Use this code:
l_mur.Remove(l_mur.Find(f => (f.xid == tmur.xid && f.sid == tmur.sid && f.mid == tmur.mid && f.bid == tmur.bid)));