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)
Related
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();
}
I am somewhat of a beginner programmer. What I am trying to do here is to check that if there is a time, if it is selected, and if that time is equivalent to the other. If that is all true then I want to skip the block of code under it. Here is the code example:
if (currentGVR.Round_Start_Time)
{
if (tr.StartLunchDateTime != null && currentGVR.Round_Start_Lunch && roundedStart == roundedStartL)
// skip
else
{
key = tr.TransactionID;
TransactionRecords[key]["Start_DateTime"] = roundedStart;
}
}
I thought about using an OR operator, but I can see where an error would occur if there was no time to compare to. Using the AND operator avoids this dilemma here.
So the overall question is, is it proper coding to negate all of the conditions to get the correct result, e.g. if (!( cond's )), and also, would this be the best way to check if there is a value to compare with before actually comparing it in C# and otherwise? The times can be null (or do not exist) in some records. Any recommendations?
I'd negate all those conditions and switch the && to || so it's more quickly evident under what conditions the code will (or will not) execute.
Plus (in my experience), you don't typically see an empty if block with all the code under the else.
if (tr.StartLunchDateTime == null || !currentGVR.Round_Start_Lunch || roundedStart != roundedStartL)
{
key = tr.TransactionID;
TransactionRecords[key]["Start_DateTime"] = roundedStart;
}
The statement
if (tr.StartLunchDateTime != null && currentGVR.Round_Start_Lunch && roundedStart == roundedStartL){
// skip
}
else
{
key = tr.TransactionID;
TransactionRecords[key]["Start_DateTime"] = roundedStart;
}
is equivalent to
if (!(tr.StartLunchDateTime != null && currentGVR.Round_Start_Lunch && roundedStart == roundedStartL))
{
key = tr.TransactionID;
TransactionRecords[key]["Start_DateTime"] = roundedStart;
}
else {
// skip
}
This can be further simplified because
!(tr.StartLunchDateTime != null &&
currentGVR.Round_Start_Lunch &&
roundedStart == roundedStartL)
Is the same as
(!(tr.StartLunchDateTime != null) ||
!(currentGVR.Round_Start_Lunch) ||
!(roundedStart == roundedStartL))
or
(tr.StartLunchDateTime == null ||
!currentGVR.Round_Start_Lunch ||
roundedStart != roundedStartL)
See DeMorgan's Laws.
if (someLongCondition)
{ }
else
{
doStuff();
}
is equivalent to this:
if (!someLongCondition)
{
doStuff();
}
So yeah, you can just negate your whole condition:
if (!(tr.StartLunchDateTime != null && currentGVR.Round_Start_Lunch && roundedStart == roundedStartL))
{ … }
But you can also pull the negation in (applying De Morgan's laws) and write it like this:
if (tr.StartLunchDateTime == null || !currentGVR.Round_Start_Lunch || roundedStart != roundedStartL)
{ … }
All these are equivalent so choose whatever makes the condition more clear (actually, consider storing it in a separate variable which you give a descriptive name).
I want to run the following query but it says that ElementAt is not supported.
List<Road> rdExist = (from u in db.Roads where (u.GPScoordinates.ElementAtOrDefault(0).Latitude == lattitude1 && u.GPScoordinates.ElementAtOrDefault(0).Longitude == longitude1) && (u.GPScoordinates.ElementAtOrDefault(1).Latitude == lattitude2 && u.GPScoordinates.ElementAtOrDefault(1).Longitude == longitude2) select u).ToList();
Can anybody please suggest an alternative to this. I can't figure out any myself. FirstOrDefault works fine but it can't help me in this query.
Try Skip(n).FirstOrDefault() in place of ElementAtOrDefault(n). That basically means "get the first element after skipping n elements", which about the same as "get element at position n" where in the latter case n starts from 0 :
List<Road> rdExist = (from u in db.Roads
where (u.GPScoordinates
.FirstOrDefault().Latitude == lattitude1 &&
u.GPScoordinates
.FirstOrDefault().Longitude == longitude1) &&
(u.GPScoordinates
.Skip(1).FirstOrDefault().Latitude == lattitude2 &&
u.GPScoordinates
.Skip(1).FirstOrDefault().Longitude == longitude2)
select u).ToList();
You can use Skip & FirstOrDefault:-
List<Road> rdExist = (from u in db.Roads where
(u.GPScoordinates.FirstOrDefault().Latitude == lattitude1 &&
u.GPScoordinates.FirstOrDefault().Longitude == longitude1) &&
(u.GPScoordinates.Skip(1).FirstOrDefault().Latitude == lattitude2 &&
u.GPScoordinates.Skip(1).FirstOrDefault().Longitude == longitude2) select u).ToList();
ElementAtOrDefault is not supported by LINQ-SQL. Check this.
Also, please be careful while using this query as it may result in Null reference exception in case of default values. So, better check for nulls before retrieving values.
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)));
I have a page in which I have several textboxes in order to search depending the value of the textboxes,If I make the search eith only one value everithing works fine but if I try to combine 2 or more values I only get the result of the last textbox.
Here's my query hope you could help me.
var query = from m in SolContext.Menores
where ((m.Solicitud.fiIdSolicitud == rdTxtFolio.Value) || (m.Solicitud.fiAnioSolicitud == rdTxtAnioFolio.Value)
|| (m.Solicitud.CTEdoSolicitud.fcDescEdoSol == status) || (m.Solicitud.fiCircuito == cto) || (m.Solicitud.fiCiudad == cd)
|| (m.Solicitud.fcCveAdsc == adsc) || (m.Solicitud.fiExpEmpleado == rdTxtExp.Value) || (m.Solicitud.fcNomEmpleado == rdTxtNom.Text)
|| (m.Solicitud.fcPatEmpleado == rdTxtAPat.Text) || (m.Solicitud.fcMatEmpleado == rdTxtAMat.Text) || (m.fcPatMenor == rdTxtAPatMenor.Text)
|| (m.fcMatMenor == rdTxtAmatMenor.Text) || (m.fcNomMenor == rdTxtNomMenor.Text) || (m.fiSexoMenor == sexo) || (m.fiAnosMenor == rdTxtAniosMenor.Value) || (m.fiMesesMenor == rdTxtMesMenor.Value))
select m;
rgSolic.DataSource = query;
rgSolic.Rebind();
My guess is that the result of the first textbox is included in the result of the second one.
My guess is that you're using or. The first textbox that matches your value will end your search. If you want to check if it matches all your values, use and.
If that doesn't solve it, we'll need more info.