Trying to change an element in a Collection - c#

I'm trying to modify this code. I need to check for a value in a specific element of FilteredCheckObservable and if true, change the value of another part of that element.
Basically something like
if (FilteredCheckObservable items.Lang = 'ENG')
{items.check = newcheckname;}
Then this will update the sourceGroups Collection.
if (string.IsNullOrEmpty(departmentLine.LineID) || string.IsNullOrEmpty(departmentLine.LineName))
continue;
bool discovered = false;
foreach (var group in sourceGroups)
{
if (!group.Key.Line.IsEqual(departmentLine))
continue;
group.Key.IsDiscovered = true;
discovered = true;
group.Key.ScheduleStatusCount = group.CountGroupItem;
break;
}
if (discovered == false)
{
var _ScheduleItemObservable = new ScheduleItemObservable(departmentLine, MainViewViewModel, Shift.ToString());
var item = new Grouping<ScheduleItemObservable, FilteredCheckObservable>(_ScheduleItemObservable);
if (IsShiftValid)
{
item.Key.Shift = Shift.ToString();
item.Key.IsHistoryEnabled = true;
}
sourceGroups.Add(item);
}
for (int index = 0; index < sourceGroups.Count; index++)
{
if (sourceGroups[index].Key.IsDiscovered == true)
{
foreach (var group in sourceGroups)
{
foreach (FilteredCheckObservable items in group)
{
if (items.Lang_ID == LanguageService.Instance.LanguageType.ToString())
{
sourceGroups.Clear();
sourceGroups.Add(item);
}
}
}
}
}

Welcome Travis,
I would wager the guilty culprit is your foreach loop.
https://stackoverflow.com/a/759985/3403999
You aren't allowed to modify a collection inside of a foreach loop.
I don't know if your collection has an indexer, but if it does, you can convert to a for loop:
for (int i = 0; i < sourceGroups.Count; i++)
{
var group = sourceGroups[i];
// The rest of your code.
I could be wrong. You do say you are trying to modify some existing code. Is it some code that is online? Maybe you could link to it to provide a full context.
Based on your new snippet, you need two for loops:
for (int index = 0; index < sourceGroups.Count; index++)
{
if (sourceGroups[index].Key.IsDiscovered == true)
{
//foreach (var group in sourceGroups)
for (int j = 0; j < sourceGroups.Count; j++)
{
var group = sourceGroups[j];
foreach (FilteredCheckObservable items in group)
{
if (items.Lang_ID == LanguageService.Instance.LanguageType.ToString())
{
sourceGroups.Clear();
sourceGroups.Add(item);
}
}
}
}
}
^ Although that might still be a bad loop. Primarily because you have sourceGroups.Clear();.
What you might be better off doing is creating an internal collection called say 'results'. Do your loop looking for your conditions, and if they meet, add that item to the results collection.
Once your loops terminate, then call sourceGroups.Clear(), and then sourceGroups.AddRange(results). If sourceGroups doesn't have an AddRange, then one final loop of:
foreach (var group in results) { sourceGroups.Add(group); }

Related

Custom Objects Listbox Foreach Loop

I have a list of postcodeitems and I bind this to a listbox.
It contains two properties : postcode, area.
I want to loop through the listbox items and if the item is selected, add the postcodeitem object to another list.
List<Valueobjects.postcodeitem> temp = _BL.GetPostCodeAreasFromZones();
PCList.AddRange(temp);
ListBox1.DataSource = PCList;
ListBox1.DataBind();
List<Valueobjects.postcodeitem> postcodecollection = new List<Valueobjects.postcodeitem>();
foreach (ListItem listitem in ListBox1.Items)
{
if (listitem.Selected)
{
i = i + 1;
//Run at 20 to speed up query
if (i == 20)
{
//Get data
CustList.AddRange(BL.SearchCustomerByPostcodeArea(postcodecollection,2));
i = 0;
}
else
{
//add the post code to temp list
postcodecollection.Add(listitem);
}
}
}
if (i > 0)
{
//get data
CustList.AddRange(BL.SearchCustomerByPostcodeArea(postcodecollection,2));
}
}
Obviously where I am trying to add (listitem) isnt going to work as this is a list item and not a postcodeitem. My question is how do I get the postcodeitem object within the list where the list item is selected?
thanks
Try something like following.
IList<Employee> boundList = ListBox1.DataSource
var obj = boundList[ListBox1.SelectedIndex]
Update => I have not tested the code but something like following. Using for loop to track the element index.
for (int i = 0; i< ListBox1.Items.Length; i++)
{
if (ListBox1.Items[i].Selected)
{
i = i + 1;
//Run at 20 to speed up query
if (i == 20)
{
//Get data
CustList.AddRange(BL.SearchCustomerByPostcodeArea(postcodecollection,2));
i = 0;
}
else
{
//add the post code to temp list
postcodecollection.Add(ListBox1.DataSource.ToList().ElementAt(i));
}
}
}
if (i > 0)
{
//get data
CustList.AddRange(BL.SearchCustomerByPostcodeArea(postcodecollection,2));
}
}
Anyways this is not recommended way. You should get selected value and use that selected value(unique field) to fetch relevant data from any persistant storage like database.

Loop and update object value in a collection using LINQ

I want to iterate through each object in a collection and then update a property on each object with another array collection
My use case is I have some entities which I initially read from my collection into an array, and perform some complex calculation and then update back my original collection.
public void WhatIfCalculation(string Product, string SiteID, System.Linq.IQueryable<DAL.OMS_StockStatus> prodStatus, ref System.Linq.IQueryable<PivotViewModel> activityInfo)
{
var sff = activityInfo.Where(a => a.Activity == "System Forecast" && a.SiteID == SiteID).ToList();
aSFF = new double?[sff.Count()];
for (int i = 0; i < sff.Count(); i++)
{
aSFF[i] = sff.ElementAt(i).Value + 1;
}
var prf = activityInfo.Where(a => a.Activity == "Planned Receipts" && a.SiteID == SiteID).ToList();
aPRF = new double?[prf.Count()];
for (int i = 0; i < prf.Count(); i++)
{
aPRF[i] = prf.ElementAt(i).Value + 2;
}
// Will perform some calculation here. And then update back my collection.
for (int i = 0; i < aSFF.Length; i++)
{
activityInfo.Where(a => a.Activity == "System Forecast" && a.SiteID == SiteID).ToList().ElementAt(i).Value = aSFF[i];
}
for (int i = 0; i < aPRF.Length; i++)
{
activityInfo.Where(a => a.Activity == "Planned Receipts" && a.SiteID == SiteID).ToList().ElementAt(i).Value = aPRF[i];
}
}
But above update is not updating my collection. When I browse back through my activityInfo - its still showing original value.
Try the following:
public void WhatIfCalculation(string Product, string SiteID, List<DAL.OMS_StockStatus> prodStatus, List<PivotViewModel> activityInfo)
{
var sff = activityInfo.Where(a => a.Activity == "System Forecast"); // Do this in the calling function >> .Where(a => a.SiteID == SiteID).ToList();
aSFF = new double?[sff.Count()];
for (var i = 0; i < sff.Count(); i++)
{
aSFF[i] = sff.ElementAt(i).Value + 1;
}
var prf = activityInfo.Where(a => a.Activity == "Planned Receipts");
aPRF = new double?[prf.Count()];
for (var i = 0; i < prf.Count(); i++)
{
aPRF[i] = prf.ElementAt(i).Value + 2;
}
// Will perform some calculation here. And then update back my collection.
for (var i = 0; i < aSFF.Length; i++)
{
sff.ElementAt(i).Value = aSFF[i];
}
for (var i = 0; i < aPRF.Length; i++)
{
prf.ElementAt(i).Value = aPRF[i];
}
}
As you can see I am sending a list instead of an IQueryable to the method. Reason is that the IQueryable is in fact an query statement and not the record itself.
Basically what you are doing is sending a query to the method which you than use to select the items and change them. The actual items are not changed.
Using the below example you are sending a list of the actual items. The items can than be changed. I also removed some double select statements from your method to enhance the speed.
this should also be useful: Differences between IQueryable, List, IEnumerator?

CheckBoxList loop not working

I'm trying to select values in a CheckBoxList control based on a data source. I have five items in the CheckBoxList and three items in the data source, but in the loop I only get one item selected.
if (ddlUserId.SelectedIndex != 0)
{
RoleDetails rd;
rd = CatalogAccess.GetSingleUserRole(ddlUserId.SelectedValue.ToString());
for (int i = 0; i < cblRoles.Items.Count; i++)
{
cblRoles.Items.FindByValue(rd.RoleID.ToString()).Selected = true;
}
}
I tried this, but it still selects only one item:
RoleDetails rd;
for (int i = 0; i < cblRoles.Items.Count; i++)
{
rd = CatalogAccess.GetSingleUserRole(ddlUserId.SelectedValue.ToString());
if (cblRoles.Items[i].Value == rd.RoleID.ToString())
cblRoles.Items[i].Selected = true;
}
CheckboxList bind code
cblRoles.DataSource = CatalogAccess.GetRoles();
cblRoles.DataTextField = "RoleDetails";
cblRoles.DataValueField = "RoleId";
cblRoles.DataBind();
When you use for loop you need to use index value (Here it is "i"), like
for (int i = 0; i < cblRoles.Items.Count; i++)
{
if(cblRoles.Items[i].Value == rd.RoleID.ToString())
cblRoles.Items[i].Selected = true;
}
Or you can use foreach as below:
Here i have created looping through items of checkbox list using foreach & item will be made selected id its value will match RoleId .
foreach (ListItem li in cblRoles.Items)
{
if (rd.RoleID.ToString() == li.Value)
{
li.Selected = true;
}
else
{
li.Selected = false;
}
}

Object reference not set to an instance of an object exception when assigning a list to a listbox

I wrote the following method to load a list box with values that have not been loaded already but I am getting an Object reference not set to an instance of an object exception when assigning the following. Any information would be helpful. Thanks.
lbxCabinetName.Items.Add(cabinetsCurrentNotUsed[i].ToString());
// Defined outside a method
List<string> cabinetsCurrentNotUsed;
// Set value in the constructor
cabinetsCurrentNotUsed = new List<string>();
Here is the whole procedure.
private void selectCabinetToAdd()
{
// Loop through all server and form cabinet types to see if there are matches
for (int x = 0; x < opticalFile.serverCabinetNames.Count; x++)
{
bool cabinetExists = false;
for (int i = 0; i < opticalFile.CabinetValues.Count; i++)
{
if (opticalFile.serverCabinetNames[x].ToString() == opticalFile.CabinetValues[i].ToString())
{
cabinetExists = true;
}
}
// Add cabinets not used to cabinetsCurrentNotUsed List
if (!cabinetExists)
{
cabinetsCurrentNotUsed.Add(opticalFile.serverCabinetNames[x].ToString());
}
}
// Send cabinetsCurrentNotUsed List to list box
for (int i = 0; i < cabinetsCurrentNotUsed.Count; i++)
{
lbxCabinetName.Items.Add(cabinetsCurrentNotUsed[i].ToString());
}
}
You are trying to add a null to the listbox.
Insted of
for (int i = 0; i < cabinetsCurrentNotUsed.Count; i++)
{
lbxCabinetName.Items.Add(cabinetsCurrentNotUsed[i].ToString());
}
use
foreach (string s in cabinetsCurrentNotUsed)
{
if(s != null)
lbxCabinetName.Items.Add(s);
}
NOTE
This part is not relavant to the question. But in your inner for loop after setting cabinetExists = true; you can break out of the inner loop (if atleast one condition is met you can make sure the cabinetExists is true. you don't have to check for the rest of the items in the inner loop)
EDIT
private void selectCabinetToAdd()
{
foreach (string sc in serverCabinetNames)
{
bool cabinetExists = false;
foreach (string cv in CabinetValues)
{
if (sc == cv)
{
cabinetExists = true;
break;
}
}
if (!cabinetExists)
{
cabinetsCurrentNotUsed.Add(sc);
}
}
foreach (string ccnu in cabinetsCurrentNotUsed)
{
if (ccnu != null)
lbxCabinetName.Items.Add(ccnu);
}
}
Also if your listBox can be null, make sure you check that first before populating the listbox.
if(lbxCabinetName != null)
{
selectCabinetToAdd();
}
EDIT 2
Dynamically adding control
ListBox lbxCabinetName = new ListBox();
lbxCabinetName.Location = new System.Drawing.Point(10, 55);
lbxCabinetName.Size = new System.Drawing.Size(130, 95);
this.Controls.Add(lbxCabinetName);
Strings are nullable so at some point you must be doing something similar to:
cabinetsCurrentNotUsed.Add(null);
Either that or as dbaseman said it is possible lbxCabinetName is null but I'm betting that isn't the case.
As an aside, it's not really a problem but if you're using a generic list of strings the ToString() call isn't necessary. You can just do this:
lbxCabinetName.Items.Add(cabinetsCurrentNotUsed[i]);

Add value in listbox from list of values without using for loop

I have used a ListBox in my Windows application. I got the variable iplist from a WCF server.
After that i addded that list in my ListBox, but it generated an error: "Collections modified, enumuration may not execute".
How might I solve this problem?
My code is:
foreach (ClsPC pc in iclsobj.GetPC())
{
if (listBox1.Items.Count == 0)
{
listBox1.Items.Add(pc.IPAddress);
}
else
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
if (!listBox1.Items[i].ToString().Contains(pc.IPAddress))
{
listBox1.Items.Add(pc.IPAddress);
}
}
}
}
You can't add to an enumeration, in this case your listbox, whilst you are iterating over the collection.
You probably want something like:
using System.Linq;
...
foreach (ClsPC pc in iclsobj.GetPC())
{
if (listBox1.Items.Count == 0)
{
listBox1.Items.Add(pc.IPAddress);
}
else
{
if (!listBox1.Items.Any(i => String.Compare(i.ToString(), pc.IPAddress, true) == 0))
{
listBox1.Items.Add(pc.IPAddress);
}
}
}
Your problem is exactly what james says, you can't add to an enumeration while iterating over the collection. Though you could also solve it in this way. (I'm assuming that pc.IPAddress is a string, if they're something else, just switch the type.)
foreach (ClsPC pc in iclsobj.GetPC())
{
if (listBox1.Items.Count == 0)
{
listBox1.Items.Add(pc.IPAddress);
}
else
{
var toAdd = new List<string>();
for (int i = 0; i < listBox1.Items.Count; i++)
{
if (!listBox1.Items[i].ToString().Contains(pc.IPAddress))
{
toAdd.Add(pc.IPAddress);
}
}
toAdd.ForEach(item => listBox1.Items.Add(item));
}
}
If you read your code it is probably not what you want.
You want to add the IP address to the list if it not exists right?
for (int i = 0; i < listBox1.Items.Count; i++)
{
if (!listBox1.Items[i].ToString().Contains(pc.IPAddress))
{
listBox1.Items.Add(pc.IPAddress);
}
}
What you do now is loop over the items in the listbox. if an item does not contain the ipaddress you will add it to the listbox.
What you want is add the ipaddress when it's not occuring the the whole listbox. Therefore:
//IPAddress is a string?
if (!listBox1.Items.Contains(pc.IPAddress))
{
listBox1.Items.Add(pc.IPAddress);
}

Categories