The attribute value of a set is changed according to the condition by using a sentence of Linq. Other values remain unchanged.

Source: Internet
Author: User

The attribute value of a set is changed according to the condition by using a sentence of Linq. Other values remain unchanged.

The first time I published a blog post. --

I was bored and saw a friend in the Group asking a question. No one answered the question in the evening, so I opened VS silently and knocked on the code.

Q: How can I write the Number + 1 in the Set newFoods whose Name contains "steamed stuffed bun" in one sentence?

List <Food> newFoods = new List <Food> {new Food {Money = 10, Name = "Tianjin steamed stuffed bun", Number = 1}, new Food {Money = 10, name = "Dog ignores steamed stuffed bun", Number = 1}, new Food {Money = 10, Name = "steamed stuffed bun", Number = 1}, new Food {Money = 10, name = "steamed bread", Number = 1}, new Food {Money = 10, Name = "zongzi", Number = 1 }};

I first typed the normal method and soon came out.

However, I still don't know how to write data using Linq. Instead, the more difficult it is to write data, the more I want to filter out an object and then merge the two objects.

Later, we found that the original object can be directly returned after query in the select statement. Previously, I thought that only model. Select (o => o. property = xxx) could be used in this way, and I didn't go into details. It seems that the solution is not enough.

The Code provided on the first floor is more concise and fast, and it is convenient to use Linq.

The final code:

1 # region 2 public class Food 3 {4 public string Name {get; set;} 5 public int Money {get; set;} 6 public int Number {get; set ;} 7} 8 static void Example1 () 9 {10 List <Food> newFoods = new List <Food> {11 new Food {Money = 10, Name = "Tianjin steamed stuffed bun ", number = 1}, 12 new Food {Money = 10, Name = "", Number = 1}, 13 new Food {Money = 10, Name = "小 ", number = 1}, 14 new Food {Money = 10, Name = "Steamed bread", Number = 1}, 15 new Food {Money = 10, Name = "zongzi", Number = 1} 16}; 17 // write 118 newFoods in Linq format. forEach (o => 19 {20 o. number = o. name. contains ("steamed stuffed bun ")? O. number + 1: o. number; 21}); 22 // write the 223 newFoods = newFoods. select (o => 24 {25 o. number = o. name. contains ("steamed stuffed bun ")? O. number + 1: o. number; 26 return o; 27 }). toList (); 28 // common syntax 29 for (int I = 0; I <newFoods. count; I ++) 30 {31 newFoods [I]. number = newFoods [I]. name. contains ("steamed stuffed bun ")? NewFoods [I]. number + 1: newFoods [I]. number; 32} 33 // print the output 34 foreach (Food food in newFoods) 35 {36 Console. writeLine ("name:" + food. name + ", price:" + food. money + ", quantity:" + food. number); 37} 38 Console. readLine (); 39} 40 # endregion

 

Related Article

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.