Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, June 30, 2010 1:47 PM
I have a list of items containing my own custom class:
List<MyClass> myList = new List<MyClass>();
MyClass consists of:
int seq;
string name;
string cssClass;
The list is populated via a linq query.
I check if the name is equal to the current page name and if so update the value cssClass. This will only happen with 1 out of approx 10 records.
I thought about iterating through the list with a for each, but can't update myList.cssClass as it is read only.
I therefore have 2 questions: 1) Is a foreach the right way of tackling this?; 2) How would I go about updating the list value?
Thanks in advance.
John
All replies (9)
Wednesday, June 30, 2010 2:38 PM ✅Answered
For that code to work you have to create properties in your MyClass as well
private int m_seq;
private string m_name;
private string m_cssClass;
public int Seq
{
get
{
return m_seq;
}
set
{
m_seq = value;
}
}
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}
public string CssClass
{
get
{
return m_cssClass;
}
set
{
m_cssClass = value;
}
}
and then try this:
myList.Where(a=>a.Name==((Page)(HttpContext.Current.Handler)).Title).ToList().ForEach(b=>b.CssClass="PageCssClassName");
Wednesday, June 30, 2010 2:12 PM
If CssClass is a property, You can achieve like this
myList.Where(a=>a.name==((Page)(HttpContext.Current.Handler)).Title).ToList().ForEach(b=>b.CssClass="PageCssClassName");
Wednesday, June 30, 2010 2:29 PM
Hi,
Thanks for the quick reply.
Taking the code as is I still get the error cssClass can't be assigned to as it is read only.
Any more ideas?
Thursday, July 1, 2010 5:29 AM
I forgot my class only had GET and not SET. Code is great - thank you!
Cheers
John
Wednesday, October 13, 2010 6:33 AM
Hi Santhosh,
I was also looking for the solution you proposed, but what if i also want to update the name... I tried to separte with comma(,) and others like (;) it didn't work... so i would like to change in that foreach loop (cssclass & name) e.g.
please advice,
thanks,
mesut
Wednesday, October 13, 2010 10:20 AM
You can create a function to update both name and css class. Use that function in for each
private void setValue(List list, string strName, string strCssClass)
{
list.Name = strName;
list.CssClass = strCssClass;
}
myList.Where(a => a.Name == ((Page)(HttpContext.Current.Handler)).Title).ToList()
.ForEach(b => setValue(b, "SOMENAME", "SOMECSSNAME"));
where List is my class name, it has to be replaced with your class name.
Tuesday, May 10, 2011 8:43 AM
I get erros when I try to follow your advice, but maybe I'm doing something wrong in my code. See below my code and the erros. 95 and 96
I think when I pass 'b' to my function what is the type?
//The whole idea is...available items is bound to the asp:Listview and it has 4 counter fields and those are not filled. So I need to get those 4 counter fields from selectedItems and update the availableitems list. Both list "availableitems" and "selecteditems" have the same structure.
selectedItems has mstly less records. So I loop over the selected items and try to get the 4 counter values from this list and tr
Problems:
List list -> wording List is unkwonwn in SetValue function so I change to (WebshopInfo list)
But I get compiling errors:
Error 95 The best overloaded method match for 'System.Collections.Generic.List<CustomModulesModules.WebShop.WebShopInfo>.ForEach(System.Action<CustomModulesModules.WebShop.WebShopInfo>)' has some invalid arguments C:\inetpub\wwwroot\CNC\DesktopModules\WebShop\ViewWebShop.ascx.cs 191 71 WebShop
Error 96 Argument '1': cannot convert from 'lambda expression' to 'System.Action<CustomModulesModules.WebShop.WebShopInfo>' C:\inetpub\wwwroot\CNC\DesktopModules\WebShop\ViewWebShop.ascx.cs 191 89 WebShop
rotected void Listview1_DataBound(object sender, EventArgs e)
{
List<WebShopInfo> availableItems = (List<WebShopInfo>)Listview1.DataSource;
List<WebShopInfo> selectedItems = Session["Basket"] as List<WebShopInfo>;
foreach (var item in selectedItems)
{
availableItems.Where(a=>a.cd_type_pub == item.cd_type_pub && a.no_pub == item.no_pub ).ToList().ForEach(b => SetValue(b,
b.Counter1,
b.Counter2,
b.Counter3,
b.Counter4, ));
}
}
//I have replace the code below "List list" to - "WebShopInfo list' in my code because "List " is unknown.
public void SetValue(List list, int Counter1, int Counter2, int Counter3, int Counter4)
{
list.Counter1= Counter1;
list.Counter2= Counter2;
list.Counter3= Counter3;
list.Counter4= Counter4;
}
z
Tuesday, May 10, 2011 5:54 PM
Just ignore the syntax details, and the fact that it doesn't compile for a while.
You have a method like this
public void SetValue(List list, int Counter1, int Counter2, int Counter3, int Counter4)
{
list.Counter1 = Counter1;
list.Counter2 = Counter2;
list.Counter3 = Counter3;
list.Counter4 = Counter4;
}
And you call that method like this
SetValue(b, b.Counter1, b.Counter2, b.Counter3, b.Counter4)
Read carefully and just think... This method call would not change anything.
Wednesday, May 11, 2011 4:21 AM
Hi Anders,
you're great. I was thinking what you wrote and indeed, I had to replace "b" with "item" as you said (nothing will be update) so I find that I was updating the list it self... and I had a comma at the end.
so syntax is ok and the code is ok...
thank you so much for replying and giving me the hint "nothing is update"
below is the whole code which is working: in case someone needs.
public void FucntionX()
{
availableItems.Where(a=>a.cd_type_pub == item.cd_type_pub &&
a.no_pub == item.no_pub &&
a.no_suite_pub == item.no_suite_pub &&
a.cd_langue == item.cd_langue).ToList().ForEach(b => SetValue(b,
item.AantalB,
item.AantalD,
item.AantalF,
item.AantalN
));
}
}
public void SetValue(WebShopInfo list, int AantalB, int AantalD, int AantalF, int AantalN)
{
list.AantalB = AantalB;
list.AantalD = AantalD;
list.AantalF = AantalF;
list.AantalN = AantalN;
}