Add or delete the query in list

Source: Internet
Author: User

Public class cartitem {
Public int proid {Get; set ;}
Public String proname {Get; set ;}
Public int num {Get; set ;}

}

List <cartitem> List = new list <cartitem> ();
// Add function
For (VAR I = 0; I <10; I ++)
List. Add (New cartitem ()
{
Proid = I,
Proname = "Product xx" + I,
Num = 3
});
// Add end

Response. Write ("============= after modification ============== ");
Response. Write ("<br/> ");
// Change the number of shopping items with the product ID 3 to 880
List. foreach (t => {If (T. proid = 3) T. num = 800 ;});
List. foreach (x =>
{
Response. Write (X. proid + "" + X. proname + "" + X. Num );
Response. Write ("<br/> ");
});

List. foreach (t => {If (T. proid = 4) List. Remove (t );});

Response. Write ("============= after deletion ============== ");
Response. Write ("<br/> ");
List. foreach (x =>
{
Response. Write (X. proid + "" + X. proname + "" + X. Num );
Response. Write ("<br/> ");
});

Response. Write ("============== If yes, the number of updates after ================== ");
Response. Write ("<br/> ");
VaR newlist = List. Where (t => T. proid = 99). tolist ();
If (newlist. Count> 0) List. foreach (t => {If (T. proid = 99) T. num = T. Num + 1 ;});
Else list. Add (New cartitem (){
Proid = 99,
Proname = "new name ",
Num = 1
});

List. foreach (x =>
{
Response. Write (X. proid + "" + X. proname + "" + X. Num );
Response. Write ("<br/> ");
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.