I have the below code that gets the data as datatable in sub class.Then i sorted the data with some if conditions in the foreach statement.I need to inform the changes when it met the condition in if statement to the subscriber.But if one condion met in the foreach statement,it inform the subscriber.Then it doesn't return back to the loop after looping the first row .Any one know how to do this?
public class Data
{
public string ID { get; set; }
public string Description { get; set; }
}
public delegate void LogHandler(Data log);
public class ClsSub
{
public event LogHandler OnDataRetrieved;
public void LoadData()
{
DataTable dt = GetData();
foreach (DataRow row in dt.Rows)
{
Data logdata = new Data();
if (row["Description"].ToString().Contains("User found"))
{
logdata.ID = row["UserID"].ToString();
logdata.Description = row["Description"].ToString();
}
if (row["Description"].ToString().Contains("User not found"))
{
logdata.ID = row["UserID"].ToString();
logdata.Description = row["Description"].ToString();
}
if (OnDataRetrieved != null)
{
OnDataRetrieved(logdata);
}
}
}
private DataTable GetData()
{
var result = new DataTable();
result.Columns.Add("UserID", typeof(string));
result.Columns.Add("Description", typeof(string));
result.Rows.Add("user1", "description1");
result.Rows.Add("user2", "description2");
return result;
}
}
public class ClsMain
{
public event LogHandler OnDataRetrieved;
public void ReadData()
{
ClsSub sub = new ClsSub();
sub.OnDataRetrieved += OnDataRetrieved;
sub.LoadData();
}
}
public class Program
{
static void Main(string[] args)
{
ClsMain main = new ClsMain();
main.OnDataRetrieved += ClsApplication_OnDataRetrieved;
main.ReadData();
Console.ReadKey();
}
private static void ClsApplication_OnDataRetrieved(Data log)
{
Console.WriteLine(log.ID + " " + log.Description);
}
}