iNetBlazer
asked on
How to check for a specific value in List<T> foreach loop
Hello Experts,
Need help checking for a specific value in List<T> foreach loop. If there is specific value then display a specific string value.
For example, how do I…
How would I incorporate the "If" statement within the “foreach” loop?
See code
When I write the "if" statment within the foreach loop I get an error...
"only assignment expression can be used as statement".
and I get other errors if I re-write the "If" statement.
Thank you for your help.
Need help checking for a specific value in List<T> foreach loop. If there is specific value then display a specific string value.
For example, how do I…
If (value.something_2 == “Null”)
{
value.something_2 == “.“;
}
Elseif (value.something_2 == “ “)
{
value.something_2 == “0”;
}
How would I incorporate the "If" statement within the “foreach” loop?
See code
protected void MyReport(string filename, IMyRepository repository)
{
using (FileStream fileStream = new FileStream(Server.MapPath(@"~/Includes/") + filename, FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(fileStream))
{
List<Report> _report = repository.GetMyReport().ToList();
foreach (var value in _report)
{
String row01 = String.Format("{0, -10}{1, 23}{2, 120}{3, 8}", value.somthing_1, values.something_2, value.something_3);
String row02 = String.Format("{0, -10}{1, 23}{2, 120}{3, 8}", value.somthing_4, values.something_5, value.something_6);
Writer.WriteLine(row01);
Writer.WriteLine(row02);
}
}
writer.Close();
}
}
When I write the "if" statment within the foreach loop I get an error...
"only assignment expression can be used as statement".
and I get other errors if I re-write the "If" statement.
Thank you for your help.
Sorry, ignore that previous comment. I misunderstood what you wanted.
instead of:
instead of:
foreach (var value in _report)
{
you would be better to loop through this way (because the compiler doesn't like the object in the foreach being modified whilst in the foreach loop):for(int i = 0; i < _report.Length; i++)
{
var value = _report[i];
If the 'T' is a reference type changing the value inside the object will change the value in the original object. Is that what you wanted? If not then you need to extract the individual contents into local strings and modify those.
ASKER
Ok, Keeping in mind that the values are of type decimal and is returned by SQL Db via EntityFramework. There are values within the Db that have "Null" and/or Whitespace. I need the check for this and if the value have "Null" then display "." is the value has Whitespace then display "0".
string s2;
if (value.something_2 == “Null”)
{
s2 == “.“;
}
else if (value.something_2 == “ “)
{
s2 == “0”;
}
else
s2 = value.something_2.ToString ();
if (value.something_2 == “Null”)
{
s2 == “.“;
}
else if (value.something_2 == “ “)
{
s2 == “0”;
}
else
s2 = value.something_2.ToString
ASKER
Get error...
Operator '==' cannot be applied to operands of type 'decimal?' and 'string'
Operator '==' cannot be applied to operands of type 'decimal?' and 'string'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You should use Report instead of var in your foreach loop. So, instead of:
You should write it:
foreach (var value in _report)
You should write it:
foreach (Report value in _report)
@The_eagle, why should that have an effect? There is no difference in those two foreach statements.
Excerpt from: http://msdn.microsoft.com/en-us/library/bb383973.aspx
Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of i are functionally equivalent:
var i = 10; // implicitly typed
int i = 10; //explicitly typed
Excerpt from: http://msdn.microsoft.com/en-us/library/bb383973.aspx
Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of i are functionally equivalent:
var i = 10; // implicitly typed
int i = 10; //explicitly typed
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@Guru and @Ace,
You both pointed me in the right direction structuring the "If" statement. Refer to the below code as the solution resulting from both your suggestions.
In referencing the code keep in mind the Db has a column of type decimal and it can have a value or Null.
Thank you for your help.
You both pointed me in the right direction structuring the "If" statement. Refer to the below code as the solution resulting from both your suggestions.
In referencing the code keep in mind the Db has a column of type decimal and it can have a value or Null.
foreach (var value in report)
{
string dot = ".";
string reportValue;
if (!value.someDecimalValue)
{
reportValue = dot;
}
else
{
reportValue = value.someDecimalValue.ToString();
}
//Other code here
}
Thank you for your help.
I'm not clear.
Are you still getting errors?
Is something not working the way you expect it to?
Are you still getting errors?
Is something not working the way you expect it to?
It depends how are you filling the List<Report> from the database. If this decimal property in C# is nullable, then you can check if null
value.someDecimalValue == null
, if not, then you might need to check it with the
Decimal.MinValue
, so it becomes
value.someDecimalValue == Decimal.MinValue
ASKER
Correction... I forgot to wright "HasValue"
foreach (var value in report)
{
string dot = ".";
string reportValue;
if (!value.someDecimalValue.HasValue)
{
reportValue = dot;
}
else
{
reportValue = value.someDecimalValue.ToString();
}
//Other code here
}
foreach (var value in _report)
{
Writer.WriteLine(String.Fo
Writer.WriteLine(String.Fo
}