IF, better approach - c#

I'm have the
Table below and I want to count how many times a row value is greater than 2.
Please note the columns are linked, 1st column->3rd , 2nd->4th .
Sometimes I want to count if tms = "abc", and sometimes if they are opposite of abc.
Is there any different approach for this case?
var_case = 1;
var tms = "abc";
foreach (DataRow r in d.Rows)
{
gCount+= cases(r, _case, tms, true) ? 1 : 0;
// gCount+= cases(r, _case, tms) ? 1 : 0;
}
private bool cases(DataRow d, int _case, string tms = "", bool t = false)
{
var h = d.ItemArray[0].ToString();
bool res = false;
switch (_case)
{
case 1: res = (h == tms) ? ((!t) ? ((int)d.ItemArray[2] > 2) ? true : false : ((int)d.ItemArray[3] > 2) ? true : false) : ((!t) ? ((int)d.ItemArray[3] > 2) ? true : false: ((int)d.ItemArray[2] > 2) ? true : false); break;
default:
break;
}
return res;
}

You can use Linq to count the rows, making the rows counting more explicit.
var _case = 1;
var tms = "abc";
int gCount = table.Rows
.Cast<DataRow>()
.Count(r => cases(r, _case, tms, false));
In the condition, you can separate the retrieving of the column index and getting the data. It would make things less complicated.
private static bool cases(DataRow dataRow, int _case, string tms = "", bool invertedSearch = false)
{
switch (_case)
{
case 1:
string h = (string)dataRow.ItemArray[0];
int column = (h == tms) ^ invertedSearch ? 2 : 3;
return (int)dataRow.ItemArray[column] > 2;
default:
return false;
}
}

Related

What is the value of arg in Task.Factory.StartNew

I have Task.Factory.StartNew in C# code with args argument. I am not quite understand what value will arg be. So what value will args be and how the value is determined?
int count = table.Rows.Count;
Dictionary<int, string> expDic = new Dictionary<int, string>();
List<Task> taskList = new List<Task>();
for (int f = 0; f < count; f++)
{
DataRow tempformula = table.Rows[f];
string formulaSymbol = Convert.ToString(tempformula[Common.Systems.Sustainability.Constants.IndicatorFormula.Symbol]);
var t = Task.Factory.StartNew(new Action<object>((args) => {
int i = (int)args;
_sem.Wait();
StringBuilder expression = new StringBuilder(1024);
DataRow formula = table.Rows[i];
int indID = Convert.ToInt32(formula[Common.Systems.Sustainability.Constants.IndicatorFormula.IndicatorID]);
int sequence = Convert.ToInt32(formula[Common.Systems.Sustainability.Constants.IndicatorFormula.Sequence]);
int? referenceIndID = Common.Util.TryToConvertToInt32(formula, Common.Systems.Sustainability.Constants.IndicatorFormula.ReferenceIndicatorID);
decimal? val = Common.Util.TryToConvertToDecimal(formula, Common.Systems.Sustainability.Constants.IndicatorFormula.Value);
string symbol = Convert.ToString(formula[Common.Systems.Sustainability.Constants.IndicatorFormula.Symbol]);
string formulaOutputUnit = Convert.ToString(formula[Common.Systems.Sustainability.Constants.Indicator.Unit]);
if (!string.IsNullOrWhiteSpace(callerAcceptedUnit)) // added by HC on 2016-05-27
{
formulaOutputUnit = callerAcceptedUnit;
}
if (referenceIndID == null)
{
if (val == null)
{
DataSet indicator = GetIndicatorByIDFromCache(companyID, indID); // added by HC on 2017-05-12, for performance tuning.
//using (DataSet indicator = indicatorMgr.GetIndicatorByID(companyID, indID))
{
DataRow dr = indicator.Tables[0].Rows[0];
int indicatorType = Convert.ToInt32(dr[Common.Systems.Sustainability.Constants.Indicator.IndicatorType]);
if (indicatorType == (int)Common.Systems.Sustainability.Constants.IndicatorTypes.Currency
|| indicatorType == (int)Common.Systems.Sustainability.Constants.IndicatorTypes.Numeric)
{
decimal? total = SumTotal(companyID, indID, indicatorType, fromDate, toDate, formulaOutputUnit, callerIndicatorID, breakToMonthly, specifiedLocations, approvalStatus
, usageDataSet, interval
, indicatorIDsChain
, allowNull
);
expression.Append(total.ToString());
}
else if (symbol == "+"
|| symbol == "-"
|| symbol == "*"
|| symbol == "/")
{
expression.Append(symbol);
}
}
}
else
{
expression.Append(val.ToString());
}
}
else
{
string exp = GetExpression(companyID, fromDate, toDate, referenceIndID.Value.ToString(), indID.ToString(), formulaOutputUnit, breakToMonthly, specifiedLocations, approvalStatus
, usageDataSet, interval
, indicatorIDsChain
);
//expression.Append(exp);
using (DataTable dt = new DataTable())
{
// fault tolerance, for in-case users assigned symbol in the last line of formula
if (exp.EndsWith("+")
|| exp.EndsWith("-")
|| exp.EndsWith("*")
|| exp.EndsWith("/"))
{
exp = exp.Substring(0, exp.Length - 1);
}
object result = dt.Compute(exp, "");
expression.Append(result);
}
} // end if (referenceIndID == null)...
if ("IF".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| "THEN".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| "ELSE IF".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| "ELSE".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| ">".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| ">=".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| "=".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| "<=".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| "<".Equals(symbol, StringComparison.OrdinalIgnoreCase)
)
{
//--------------------------------------------------------
// Begin, added by HC on 2016-12-14
// as requested by CQS users, add "IF" statement for GRI indicators
string exp = HandleIFCondition(table, i, companyID, fromDate, toDate, indicatorIDs, callerIndicatorID, callerAcceptedUnit, breakToMonthly, specifiedLocations, approvalStatus, usageDataSet, interval, indicatorIDsChain);
expression.Append(exp);
// End, added by HC on 2016-12-14
//--------------------------------------------------------
// Find End if, added by Alex Poon on 20190306
int ifIndex = i;
for (var j = ifIndex; j < count; j++)
{
string endIfSymbol = Convert.ToString(table.Rows[j][Common.Systems.Sustainability.Constants.IndicatorFormula.Symbol]);
if ("END IF".Equals(endIfSymbol, StringComparison.OrdinalIgnoreCase))
{
i = j;
break;
}
}
}
else
{
if (!string.IsNullOrWhiteSpace(symbol)
&& expression.Length > 0 // added by HC on 2016-06-24
)
{
if (i + 1 >= count && symbol != ")")
{
// ignore the symbol
}
else if (expression.ToString() != symbol)
{
expression.Append(symbol);
}
}
else if (expression.Length == 0 && symbol == "(")
{
expression.Append(symbol);
}
if (symbol == "days")
{
// added by HC on 2017-01-23, a new requirements for calculating average values by days.
if (fromDate != null && toDate != null)
{
int days = toDate.Value.Subtract(fromDate.Value).Days + 1;
expression.Append(days);
}
else
{
expression.Append(1);
}
}
} // end if ("IF".Equals(symbol, StringComparison.OrdinalIgnoreCase)...
if (expDic != null && expression != null)
{
expDic.Add(i, expression.ToString());
}
_sem.Release();
}), f);
taskList.Add(t);
if ("IF".Equals(formulaSymbol, StringComparison.OrdinalIgnoreCase))
{
for (var j = f; j < count; j++)
{
string endIfSymbol = Convert.ToString(table.Rows[j][Common.Systems.Sustainability.Constants.IndicatorFormula.Symbol]);
if ("END IF".Equals(endIfSymbol, StringComparison.OrdinalIgnoreCase))
{
f = j;
break;
}
}
}
}
Task.WaitAll(taskList.ToArray());
List<int> keys = expDic.Keys.ToList();
keys.Sort();
string finalExpression = "";
foreach (var key in keys)
{
finalExpression += expDic[key];
}
return finalExpression;
}
args in your case is data that will be passed to a delegate specified by Action<object> parameter.
See docs.
When using overload of StartNew with Action<object>, you need to supply next argument of type object, which will be used in passed delegate.
Task.Factory.StartNew((o) => {/* 1 will be used here */}, 1, token, options, scheduler);
MSDN
The args argument (referred to as "state" in the documentation) is a parameter you need to specify when you call to Task.Factory.StartNew.
state (Object): An object containing data to be used by the action delegate.
Sample:
object param = somevalue;
Task.Factory.StartNew( (arg) =>
{ /* get the arg value here.*/
}, param);

how to check error conditions c#

I have group of conditions like
foreach
{
Condition a
condition b
}
So I am validating the values based on conditions.I am facing the problem: for example I have list of items like {1,2,3,4}. So I have a condition like if item 1 is fail then item 2,3,4 should be fail.
if item 2 is fail then item 3,4 should be fail and so on.
I am trying in below code.
foreach (SagaData item in lstPrm)
{
PriceDetail prevPrice = null;
PriceDetail currentPrice = null;
PriceDetail nextPrice = null;
bool bHasError = false;
int iPriceMasterId = 0;
int iPriceTypeId = 0;
string sMprCurrencyType = null;
string sPublisherCurrencyType = null;
int? iExpirationCalendarId = 0;
string sPRMMessage = string.Empty;
//a) If Change Indicator = C or A and Price = 0.00: Cannot change price value to zero.
if ((item.ChangeIndicator == 'C' || item.ChangeIndicator == 'A') && item.PostingPrice == 0)
{
bHasError = true;
sPRMMessage = "FAILURECannot change price value to zero";
}
//b) If Change Indicator = D and Price > 0.00: Invalid deactivation.
if ((item.ChangeIndicator == 'D') && (item.PostingPrice > 0) && (!bHasError))
{
bHasError = true;
sPRMMessage = "FAILUREInvalid deactivation";
}
so i have if condition a fail for item 1 then how should i keep maintain the error for next iteration.
Thanks for the help. if you want more info plz let me know.
You can go through your Collection with a simple for loop and use an ErrorArray:
bool[] bHasError = new bool[lstPrm.Count];
for (int i = 0; i < lstPrm.Count; i++)
{
...
bHasError[i] = true;
...
}
or you can define bHasError BEFORE the foreach if one error is enough for you to consider.

Change strings from one string to another in an array based on a condition

I have an array of strings, most of which are string values "true" or "false", there are other strings there but i don't wish to alter them.
So i wish to loop through the strings and where i encounter the word "true" or "false" i want to change the string to "1" or "0"
These strings of 1, 0 and others will end up being used as parameters passed to a SQL stored procedure IE: seCmd.Parameters["#CheckListRef"].Value = data[0];
I am merely struggling with the syntax and therefore seeking guidence
string[] data = args.Trim().Split(',');
// Loop over strings
foreach (string s in data)
{
if(s == "true")
{
convert the string True to the string 1
}
else if(s == "false")
{
convert the string True to the string 0
}
}
please can someone offer some guidance
What your code is missing is a reference back to the array location you are checking.
Using a for loop would let you keep that reference:
string[] data = args.Trim().Split(',');
// Loop over strings
for(int i = 0; i < data.Length; i++)
{
if(data[i] == "true")
{
data[i] = "1";
}
else if(data[i] == "false")
{
data[i] = "0";
}
}
Note - the above is case sensitive, so you may wan to replace the string tests with data[i].Equals("true", StringComparison.InvariantCultureIgnoreCase).
This should help:
string[] data = args.Trim().Split(',');
for(int i = 0; i < data.Length; i++)
{
if(data[i] == "true")
{
data[i] = "1";
}
else if(data[i] == "false")
{
data[i] = "0";
}
}
Also, you could shorten your code a bit by doing the following:
string[] data = args.Trim().Split(',');
for(int i = 0; i < data.Length; i++)
{
data[i].Replace("true", "0").Replace("false", "1");
}
Here is LINQ version which will return you a new array:
string[] newData = data.Select(r => (r == "true" ? "1" : r == "false" ? "0" : r))
.ToArray();
Or if you want to ignore case then:
newData = data.Select(r =>
(r.Equals("true", StringComparison.CurrentCultureIgnoreCase) ? "1"
:r.Equals("false", StringComparison.CurrentCultureIgnoreCase) ? "0"
: r))
.ToArray();
I would use a for loop for this but, in your case would this not work
if(s == "true")
{
s="1";
}
else if(s == "false")
{
s="0";
}
var data = args.Trim()
.Split(',')
.ToList()
.ConvertAll(s => s.Replace("true", "1").Replace("false", "0"));
Go for a LINQ expression:
var parsedData = data.Trim()
.Where(d => d == "true" || d == "false")
.Select(d => bool.Parse(d) ? "1" : "0")
.ToList();
string originalString = "true, True, FALSE, something true,false,something else,false";
string resultString = Regex.Replace(
originalString, #"((?<=(,\s*))|^)true((?=(\s*,))|$)", "1",RegexOptions.IgnoreCase);
resultString = Regex.Replace(
resultString, #"((?<=(,\s*))|^)false((?=(\s*,))|$)", "0", RegexOptions.IgnoreCase);
string[] resultArray = Regex.Split(resultString, #"\s*,\s*");
resultArray:
1
1
0
something true
0
something else
0
You do not have to convert it to 1 or 0. Simply do Convert.ToBoolean() if your datatype is bit then try this:
bool bln;
if(bool.TryParse(data[0], out bln))
{
seCmd.Parameters.Add("#CheckListRef",SqlDbType.Bit).Value =bln;
}

Delete and remove the row matching string?

I want to search for a string in DataTable if it starts with "null" delete this string, if this row has no values then erase this row too. Can someone show me how to do it?
ID Name Comment
2 lola hallo
5 miky hi
null0 // delete null0 and remove this row
null1 ko // delete null1 but do not remove this row
1 hub why
3 null2 //delete null2 but do not remove this row
I tried to delete "null" but it doesn't work.
My Code:
int ct1 = 0;
for (int i = 0; i < resE1.Rows.Count; i++ )
{
if(resE1.Rows[i]["SignalName"].ToString() == "null" + ct1)
{
resE1.Rows[i].SetField<String>(2, "");
}
ct1++;
}
I find a solution to replace "null*" with empty spaces, but now I want to erase this row if the cells values of this row are empty, how can i do it?
int ct1 = 0;
for (int i = 0; i < resE1.Rows.Count; i++)
{
string fix = resE1.Rows[i]["SignalName"].ToString();
if(fix.Contains("null"))
{
resE1.Rows[i].SetField<String>(2, "");
}
ct1++;
}
I tried this. Works for me.
List<DataRow> rowsToRemove = new List<DataRow>();
foreach (DataRow dr in resE1.Rows)
{
if (dr["Name"].ToString().IndexOf("null") == 0)
{
dr.SetField("Name", "");
}
bool hasValue = false;
for (int i = 0; i < dr.ItemArray.Count(); i++)
{
if (!dr[i].ToString().Equals(String.Empty))
hasValue = true;
}
if (!hasValue) rowsToRemove.Add(dr);
}
foreach(DataRow dr in rowsToRemove)
{
dr.Delete();
}
Here is your code changed so it works although I don't like how it is written. Check the answer:
int ct1 = 0;
int i = 0;
while(i < dt.Rows.Count)
{
if ( dt.Rows[i]["Name"].ToString() == "null" + ct1)
{
if ((dt.Rows[i][0] == null || dt.Rows[i][0].ToString() == string.Empty) && (dt.Rows[i][2] == null || dt.Rows[i][2].ToString() == string.Empty))
{
dt.Rows.RemoveAt(i);
i--;
}
else
{
dt.Rows[i].SetField<String>(1, "");
}
ct1++;
}
i++;
}
Assuming your DataTable has column "Name" of type string you can achieve it like this:
private DataTable RemoveUnwanted(DataTable source)
{
var rowsEnu = source.Rows.GetEnumerator();
while (rowsEnu.MoveNext())
{
((DataRow)rowsEnu.Current)["Name"] = ((DataRow)rowsEnu.Current)["Name"].ToString().StartsWith("null") ? string.Empty : ((DataRow)rowsEnu.Current)["Name"];
}
return (from i in source.AsEnumerable()
where !(i.ItemArray.All(o => string.IsNullOrEmpty(o.ToString())))
select i).CopyToDataTable();
}
You will also need reference to System.Data.DataSetExtensions.
To answer one of your questions, to delete the entire row do something like this:
if(resE1.Rows[i].Contains("null") && (resE1.Rows[i]["ID"].ToString() == "") && (resE1.Rows[i]["Comment"].ToString() == ""))
{
resE1.Rows[i].Delete();
}
bool emptyFlag = false;
if(resE1.Rows[i].Contains("null"))
{
for(int j = 0; j < resE1.Columns.Count; j++)
{
if(resE1.Rows[i][j].Equals(""))
emptyFlag = true;
else
emptyFlag = false;
}
if(emptyFlag)
resE1.Rows[i].Delete();
}
You need something like this:
String test = "Null01";
if (test.IndexOf("Null") == 0)
{
test = test.Replace("Null", String.Empty);
}
For second part of your question, just check if Comments are null or empty then replace the string too.
Arrange code according to your own requirement.

FormatException 0 is not a valid value for Boolean

I am getting a formatexception with the following code. Any one know how to make BooleanConverter convert from 0/1 to true/false.
bool bVal=true;
string sVal = "0";
Console.WriteLine(TypeDescriptor.GetConverter(bVal).ConvertFrom(sVal));
Thanks for the help!
Try the following
public static bool ConvertToBasedOnIntValue(string value) {
// error checking omitted for brevity
var i = Int32.Parse(value);
return value == 0 ? false : true;
}
Or you could use the following which won't throw exceptions but it will consider everything that is quite literally not 0 to be true
public static bool ConvertToBasedOnIntValue(string value) {
if ( 0 == StringComparer.CompareOrdinal(value, "0") ) {
return false;
}
return true;
}
If you're going to use Int32.Parse, use Int32.TryParse instead. It doesn't throw if the conversion fails, instead returning true or false. This means that it's more performant if all you're doing is checking to see if your input is a value. Example:
public static bool ConvertToBool(string value)
{
int val = 0;
return (int.TryParse(value, out val) && val == 0) ? false : true;
}
I have a tendency to go overboard on the ternary operator (x ? y : z), so here's a slightly easier-to-read version:
public static bool ConvertToBool(string value)
{
int val = 0;
if (int.TryParse(value, out val))
{
return val == 0 ? false : true;
}
return false;
}
(I tested them both. "1" returns true, "0" returns false.)
There are only two cases so can just check for them explicitly.
I just built a helper function that will handle the special case of booleans:
Private Shared Function StringConverter(ByVal colValue As String, ByVal propertyType As Type) As Object
Dim returnValue As Object
'Convert the string value to the correct type
Select Case propertyType
Case GetType(Boolean)
'Booleans need to be coded separately because by default only "True" and "False" will map to a Boolean
'value. SQL Server will export XML with Booleans set to "0" and "1". We want to handle these without
'changing the xml.
Select Case colValue
Case "0"
returnValue = False
Case "1"
returnValue = True
Case "False"
returnValue = False
Case "True"
returnValue = True
Case Else
Throw New ArgumentException("Value of '" & colValue & "' cannot be converted to type Boolean.")
End Select
Case Else
'Let .Net Framework handle the conversion for us
returnValue = TypeDescriptor.GetConverter(propertyType).ConvertFromString(colValue)
End Select
Return returnValue
End Function
Your original code was good enough, only you can't set the string value to "0" and "1". In your code, TypeConverter.ConvertFrom() will eventually invoke Boolean.TryParse(), the code for which looks like this (thanks Reflector!)
public static bool TryParse(string value, out bool result)
{
result = false;
if (value != null)
{
if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
{
result = true;
return true;
}
if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
{
result = false;
return true;
}
if (m_trimmableChars == null)
{
char[] destinationArray = new char[string.WhitespaceChars.Length + 1];
Array.Copy(string.WhitespaceChars, destinationArray, string.WhitespaceChars.Length);
destinationArray[destinationArray.Length - 1] = '\0';
m_trimmableChars = destinationArray;
}
value = value.Trim(m_trimmableChars);
if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
{
result = true;
return true;
}
if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
{
result = false;
return true;
}
}
return false;
}
So make the following change to your code and you should be good:
bool bVal = true;
string sVal = "false";
Console.WriteLine(TypeDescriptor.GetConverter(bVal).ConvertFrom(sVal));
string _sOne = "1";
string _sTrue = "TrUe";
bool _bRandom = !Convert.ToBoolean(__sTrue);
string _sResult = Convert.ToBoolean(_sOne);
string _sResultB = Convert.ToBoolean(_sTrue);
string _sResultC = _bRandom.ToString();

Categories