Compare list data with static string in C# - c#

I have the following list,
String UpDownStatus = "UP";
List<db1> StockData = new List<db1>();
db1 newStock = new db1();
newStock.Date = (DateTime)reader["Date"];
newStock.High = (double)reader["High"];
newStock.Low = (double)reader["Low"];
newStock.Close = (double)reader["Close"];
newStock.Up_Down = (string)reader["Up_Down"];
StockData.Add(newStock);
The Up_Down column in my DB has the following two string : "UP" and "DOWN"
how can i compare whether the current value of
for (int i = 0; i < StockData.Count; i++)
{
if(StockData[i].Up_Down.CompareTo(UpDownStatus) != 0)
{
//do something
}
}
I know, i could reframe this statement like if (StockData[i].Up_Down=="UP") but i need a solution where I could use .CompareTo function.

If I understood your question right and you want to do something on all items which have the same up/down-status as the variable UpDownStatus, this would be the easiest way to do it:
foreach (var item in StockData.Where(e => e.Up_Down == UpDownStatus))
{
// do something
}

Related

How can I access multi-element List data stored in a public class?

My first question on SO:
I created this public class, so that I can store three elements in a list:
public class myMultiElementList
{
public string Role {get;set;}
public string Country {get;set;}
public int Commonality {get;set;}
}
In my main class, I then created a new list using this process:
var EmployeeRolesCountry = new List<myMultiElementList>();
var rc1 = new myMultiElementList();
rc1.Role = token.Trim();
rc1.Country = country.Trim();
rc1.Commonality = 1;
EmployeeRolesCountry.Add(rc1);
I've added data to EmployeeRolesCountry and have validated that has 472 lines. However, when I try to retrieve it as below, my ForEach loop only retrieves the final line added to the list, 472 times...
foreach (myMultiElementList tmpClass in EmployeeRolesCountry)
{
string d1Value = tmpClass.Role;
Console.WriteLine(d1Value);
string d2Value = tmpClass.Role;
Console.WriteLine(d2Value);
int d3Value = tmpClass.Commonality;
Console.WriteLine(d3Value);
}
This was the most promising of the potential solutions I found on here, so any pointers greatly appreciated.
EDIT: adding data to EmployeeRolesCountry
/*
Before this starts, data is taken in via a csvReader function and parsed
All of the process below is concerned with two fields in the csv
One is simply the Country. No processing necessary
The other is bio, and this itself needs to be parsed and cleansed several times to take roles out
To keep things making sense, I've taken much of the cleansing out
*/
private void File_upload_Click(object sender, EventArgs e)
{
int pos = 0;
var EmployeeRolesCountry = new List<myMultiElementList>();
var rc1 = new myMultiElementList();
int a = 0;
delimiter = ".";
string token;
foreach (var line in records.Take(100))
{
var fields = line.ToList();
string bio = fields[5];
string country = fields[4];
int role_count = Regex.Matches(bio, delimiter).Count;
a = bio.Length;
for (var i = 0; i < role_count; i++)
{
//here I take first role, by parsing on delimiter, then push back EmployeeRolesCountry with result
pos = bio.IndexOf('.');
if (pos != -1)
{
token = bio.Substring(0, pos);
string original_token = token;
rc1.Role = token.Trim();
rc1.Country = country.Trim();
rc1.Commonality = 1;
EmployeeRolesCountry.Add(rc1);
a = original_token.Length;
bio = bio.Remove(0, a + 1);
}
}
}
}
EDIT:
When grouped by multiple properties, this is how we iterate through the grouped items:
var employeesGroupedByRolwAndCountry = EmployeeRolesCountry.GroupBy(x => new { x.Role, x.Country });
employeesGroupedByRolwAndCountry.ToList().ForEach
(
(countryAndRole) =>
{
Console.WriteLine("Group {0}/{1}", countryAndRole.Key.Country, countryAndRole.Key.Role);
countryAndRole.ToList().ForEach
(
(multiElement) => Console.WriteLine(" : {0}", multiElement.Commonality)
);
}
);
__ ORIGINAL POST __
You are instantiating rc1 only once (outside the loop) and add the same instance to the list.
Please make sure that you do
var rc1 = new myMultiElementList();
inside the loop where you are adding the elements, and not outside.
All references are the same in your case:
var obj = new myObj();
for(i = 0; i < 5; i++)
{
obj.Prop1 = "Prop" + i;
list.Add(obj);
}
now the list has 5 elements, all pointing to the obj (the same instance, the same object in memory), and when you do
obj.Prop1 = "Prop" + 5
you update the same memory address, and all the pointers in the list points to the same instance so, you are not getting 472 copies of the LAST item, but getting the same instance 472 times.
The solution is simple. Create a new instance every time you add to your list:
for(i = 0; i < 5; i++)
{
var obj = new myObj();
obj.Prop1 = "Prop" + i;
list.Add(obj);
}
Hope this helps.

How to modify the Content in RuleAction

Below is the code I have written. I need to modify the content which is available in rr
for example :
if rr.ThenAction = "this.MacroLoop.Current.Strategy.SubStrategy.Current.Block.AIN.HLOP = 0"
I need it to be modified to
"rr.ThenAction = "this.MacroLoop.Current.Strategy.SubStrategy.Current.Block.AIN.HLOP = 1"
if (!ruleSet.Validate(rv))
{
List<System.Workflow.Activities.Rules.Rule> rulesList = new List<System.Workflow.Activities.Rules.Rule>();
rulesList = ruleSet.Rules.ToList();
foreach (System.Workflow.Activities.Rules.Rule rr in rulesList)
{
// I need to modify the content in rr
}
}
Help is much appreciated.
I am not sure but are you looking for something like;
rulesList = ruleSet.Rules.ToList();
for (int i = 0; i < rulesList.Count; i++)
{
rulesList[i] = "sddfs";
if(rulesList[i].ThenAction == "this.MacroLoop.Current.Strategy.SubStrategy.Current.Block.AIN.HLOP = 0")
{
rulesList[i].ThenAction = "this.MacroLoop.Current.Strategy.SubStrategy.Current.Block.AIN.HLOP = 1";
}
}
Hope it helps...!!!
PS: if you are planning to change value of rr in foreach, then it will not work as foreach is a read only iterator that iterates dynamically classes that implement IEnumerable, but you can use item.value to make changes.

Add Multiple record using Linq-to-SQL

I want to add Multiple rows into Table using Linq to SQL
public static FeedbackDatabaseDataContext context = new FeedbackDatabaseDataContext();
public static bool Insert_Question_Answer(List<QuestionClass.Tabelfields> AllList)
{
Feedback f = new Feedback();
List<Feedback> fadd = new List<Feedback>();
for (int i = 0; i < AllList.Count; i++)
{
f.Email = AllList[i].Email;
f.QuestionID = AllList[i].QuestionID;
f.Answer = AllList[i].SelectedOption;
fadd.Add(f);
}
context.Feedbacks.InsertAllOnSubmit(fadd);
context.SubmitChanges();
return true;
}
When I add records into list object i.e. fadd the record is overwrites with last value of AllList
I'm late to the party, but I thought you might want to know that the for-loop is unnecessary. Better use foreach (you don't need the index).
It gets even more interesting when you use LINQ (renamed method for clarity):
public static void InsertFeedbacks(IEnumerable<QuestionClass.Tabelfields> allList)
{
var fadd = from field in allList
select new Feedback
{
Email = field.Email,
QuestionID = field.QuestionID,
Answer = field.SelectedOption
};
context.Feedbacks.InsertAllOnSubmit(fadd);
context.SubmitChanges();
}
By the way, you shouldn't keep one data context that you access all the time; it's better to create one locally, inside a using statement, that will properly handle the database disconnection.
You should create object of Feedback in the scope of for loop, so change your method to :
public static bool Insert_Question_Answer(List<QuestionClass.Tabelfields> AllList)
{
List<Feedback> fadd = new List<Feedback>();
for (int i = 0; i < AllList.Count; i++)
{
Feedback f = new Feedback();
f.Email = AllList[i].Email;
f.QuestionID = AllList[i].QuestionID;
f.Answer = AllList[i].SelectedOption;
fadd.Add(f);
}
context.Feedbacks.InsertAllOnSubmit(fadd);
context.SubmitChanges();
return true;
}

Foreach loop to check the empty string and assign it a value c#

I dont much idea about Foreach Loop
I have 4 String Variables and i am storing the encrypted values from app.config in those varialbles.My Work is to Detect if there is data in those 4 variables.Even if one doesnt have values i want to pass a default encrypted values to it.I started doing like this......
string EncValue = oEncrypt.EncryptBase64String("NO Data Found");
UpdateOrCreateAppSetting("UserName", ref MPScmUserName);
UpdateOrCreateAppSetting("Password", ref MPScmPassword);
UpdateOrCreateAppSetting("DBUserName", ref MPDbUserName);
UpdateOrCreateAppSetting("DBPassword", ref MPDbPassword);
var list5 = from docs in doc1.Descendants("appender").Elements()
where docs.Name == "file"
select docs;
var element5 = list5.FirstOrDefault();
MPLoadAppConfig = appConfigFile1;
MPErrorLog = element5.Attribute("value").Value;
string[] namesArray = new string[] {
MPScmUserName,MPScmPassword,MPDbUserName,MPDbPassword};
foreach (string i in namesArray)
{
if (i is string)
{
if (i == "")
{
i = EncValue.ToString();
}
}
}
How should i assign default encrypted value to the strings in that array which is a empty string. Please Help me....
You don't need to say if (i is string) because it's already a string.
In another if block you may say
if(string.IsNullOrEmpty(i))
{
}
or you may use:
string[] arr = new string[] { "","A"};
for (int i = 0; i < arr.Length; i++)
{
if (string.IsNullOrEmpty(arr[i]))
{
arr[i] = "Hello";
}
}
Looping on the string array and changing the value of current string doesn't means you are changing the value of the string inside the array. When foreach loops on your array it returns a new string instance in i, so changing that i don't change the value in namesArray.
You need a traditional for loop with an indexer to access directly to the namesArray
for(int i = 0; i < namesArray.Length; i++)
{
if (namesArray[i] == string.Empty)
{
namesArray[i] = EncValue.ToString();
}
}
No need for the if (i is string) part because you already know it's a string. You should probably also use string.IsNullOrWhiteSpace(); to test for an empty string.
Edit (shameless copy from Mark's answer):
for (int i = 0; i < namesArray.Length; ++i)
{
if (string.IsNullOrWhiteSpace(namesArray[i]))
{
namesArray[i] = EncValue.ToString();
}
}
You can't modify the variable in a foreach loop. Your code should give you the following compile error:
Cannot assign to 'i' because it is a 'foreach iteration variable'
You could do this using LINQ:
namesArray = namesArray.Select(s => s == "" ? EncValue.ToString() : s).ToArray();
Alternatively you could use a simple for loop:
for (int i = 0; i < namesArray.Length; ++i)
{
if (namesArray[i] == "")
{
namesArray[i] = EncValue.ToString();
}
}
string[] newNamesArray =
Enumerable.Select(namesArray, s => s == "" ? EncValue : s).ToArray();

C#. Set a member object value using reflection

I need your help with the following code below. Basically I have a class called "Job" which has some public fields. I'm passing to my method "ApplyFilter" two parameters "job_in" and "job_filters". First parameter contains actual data, and the second one has instructions (if any). I need to iterate through "job_in" object, read it's data, apply any instructions by reading "job_filters", modify data (if needed) and return it in a new "job_out" object. Everything works fine till i need to store my data in "job_out" object:
public class Job
{
public string job_id = "";
public string description = "";
public string address = "";
public string details = "";
}
...
private Job ApplyFilters(Job job_in, Job job_filters)
{
Type type = typeof(Job);
Job job_out = new Job();
FieldInfo[] fields = type.GetFields();
// iterate through all fields of Job class
for (int i = 0; i < fields.Length; i++)
{
List<string> filterslist = new List<string>();
string filters = (string)fields[i].GetValue(job_filters);
// if job_filters contaisn magic word "$" for the field, then do something with a field, otherwise just copy it to job_out object
if (filters.Contains("$"))
{
filters = filters.Substring(filters.IndexOf("$") + 1, filters.Length - filters.IndexOf("$") - 1);
// MessageBox.Show(filters);
// do sothing useful...
}
else
{
// this is my current field value
var str_value = fields[i].GetValue(job_in);
// this is my current filed name
var field_name = fields[i].Name;
// I got stuck here :(
// I need to save (copy) data "str_value" from job_in.field_name to job_out.field_name
// HELP!!!
}
}
return job_out;
}
Please help. I've seen a few samples by using properties, but i'm pretty sure it is possible to do the same with fields as well. Thanks!
Try this
public static void MapAllFields(object source, object dst)
{
System.Reflection.FieldInfo[] ps = source.GetType().GetFields();
foreach (var item in ps)
{
var o = item.GetValue(source);
var p = dst.GetType().GetField(item.Name);
if (p != null)
{
Type t = Nullable.GetUnderlyingType(p.FieldType) ?? p.FieldType;
object safeValue = (o == null) ? null : Convert.ChangeType(o, t);
p.SetValue(dst, safeValue);
}
}
}
fields[i].SetValue(job_out, str_value);

Categories