I have a model:
public class KPIResults
{
public virtual string Title { get; set; }
public virtual int Total { get; set; }
public virtual int Week6 { get; set; }
public virtual int Week5 { get; set; }
public virtual int Week4 { get; set; }
public virtual int Week3 { get; set; }
public virtual int Week2 { get; set; }
public virtual int Week1 { get; set; }
public virtual string stringWeek6 { get; set; }
public virtual string stringWeek5 { get; set; }
public virtual string stringWeek4 { get; set; }
public virtual string stringWeek3 { get; set; }
public virtual string stringWeek2 { get; set; }
public virtual string stringWeek1 { get; set; }
}
In my reporting repository i have:
int strfound = 0;
string str = "Number";
foreach (DataRow dr in dt.Rows)
{
string title = dr["Title"].ToString();
int total = Convert.ToInt32(dr["Total"]);
if (dr["Week6"].contains("Number") //synntacs not right?
var week6 = dr["strWeek6"]
end
or do this:
strfound = str.IndexOf(dr); //syntacs not right?
if (strfound == 0)
{
var week6 = dr["strWeek6"]
}
var week6 = Convert.ToInt32(dr["Week6"]);
var week5 = Convert.ToInt32(dr["Week5"]);
var week4 = Convert.ToInt32(dr["Week4"]);
var week3 = Convert.ToInt32(dr["Week3"]);
var week2 = Convert.ToInt32(dr["Week2"]);
var week1 = Convert.ToInt32(dr["Week1"]);
results.Add(new KPIResults() {Title = title, Total = total, Week6 = week6, Week5 = week5, Week4 = week4, Week3 = week3, Week2 = week2, Week1 = week1});
}
return results;
I should maybe try and do a string search but my code in the is is not right. I search for ways to handle string but not clear.
thanks
Somewhat unclear on what you are trying to do, to answer your question,
All I want to know is it possible to cast the var type to be stored as an int or the other way around?No.I think your best bet would be to use
stringthen it will be able to hold text and numbers. You could do a regex test to check if it contains text, then if not, convert the number it stores to anintfor you to work this.Or just make a better model that will include all the sections you need.
You could also use
.GetType()to return the type ofvar.Update
Ok, it looks like you are now trying to detect if
dr["Week6"]contains astringor anint. One way to go about this is:If
dr["Week6"]can be converted into anint, then theifstatement istrueandnumberwill contain the value ofdr["Week6"].Here is the metadata for
int.TryParse: