C # Query [Study Notes] by LINQ ],

Source: Internet
Author: User

C # Query [Study Notes] by LINQ ],
[LINQ query] case: first define two classes: Master and KongFu, and then define variables in these two classes

  1.  1 class Master 2 { 3     publicintId{ get;set;} 4     publicstringName{ get;set;} 5     publicintAge{ get;set;} 6     publicstringKongfu{ get;set;} 7     publicstringMenPai{ get;set;} 8     publicintLevel{ get;set;} 9     public overide string ToString()10    {11         return string.Fromat("Id:{0},Name:{1},Age:{2},Kongfu:{3},MenPai:{4},Level:{5}", Id, Name, Age, Kongfu, Menpai, Leve)12    }13 }    
     1 class KongFu 2 { 3    public int Id { get;set;} 4    public string Name { get;set;} 5    public int Power { get;set;} 6  7    public override string ToString() 8    { 9       return string.Format("Id:{0},Name:{1},Power:{2}", Id, Name, Power);10    }11 }

     

    1 static void Main (String [] args) 2 {3 var master = newList <Master> () 4 {5 newMaster () {Id = 1, Name = "Huang Rong ", age = 18, Menpai = "", Kongfu = "" ", Level = 9}, 6 newMaster () {Id = 2, Name =" ", age = 70, Menpai = "", Kongfu = "" ", Level = 10}, 7 newMaster () {Id = 3, Name =" Guo Jing ", age = 22, Menpai = "", Kongfu = "", Level = 10}, 8 newMaster () {Id = 4, Name = "", age = 50, Menpai = "Mingjiao", Kongfu = "sunflower Collection", Level = 1}, 9 newMaster () {Id = 5, Name = "Invincible ", age = 35, Menpai = "Mingjiao", Kongfu = "sunflower Collection", Level = 10}, 10 newMaster () {Id = 6, Name = "Lin pingzhi ", age = 23, Menpai = "Huashan", Kongfu = "sunflower Collection", Level = 7}, 11 newMaster () {Id = 7, Name = "Yue Bu ", age = 50, Menpai = "Huashan", Kongfu = "sunflower Collection", Level = 8}, 12 newMaster () {Id = 8, Name = "Ling huchong ", age = 23, Menpai = "Huashan", Kongfu = "Gu JIU Jian", Level = 10}, 13 newMaster () {Id = 9, Name = "Mei chaofeng ", age = 23, Menpai = "Plum Blossom Island", Kongfu = "jiuyin Zhenjing", Level = 10}, 14 newMaster () {Id = 10, Name = "Yellow pharmacist", Age = 23, menpai = "Plum Blossom Island", Kongfu = "", Level = 10}, 15 newMaster () {Id = 11, Name = "", Age = 23, menpai = "Huashan", Kongfu = "lone nine swords", Level = 10} 16}; 17 var kongfu = newList <KongFu> () 18 {19 newKongFu () {Id = 1, Name = "dongles", Power = 95}, 20 newKongFu () {Id = 2, Name = "", Power = 100 }, 21 newKongFu () {Id = 3, Name = "", Power = 100}, 22 newKongFu () {Id = 4, Name =" ", power = 100}, 23 newKongFu () {Id = 5, Name = "jiuyin Zhenjing", Power = 100}, 24 newKongFu () {Id = 6, name = "Power = 100}, 25}; 26}
Start to create a query:
[First]: Find the List with a Level greater than 8 to var result = new List <Master> (); foreach (var item in master) {if (item. level> 8) {result. add (item) ;}}foreach (var item in result) {Console. writeLine (item. toString (); // If ToString () is not specified, it exists by default.} Console. readKey (); // you need to create the ToString () method in the master class and KongFu, because after the query is complete, traverse (foreach) h will see the following error [2]: Use the professional query language to search and sort // from: indicates which set to search ...... Where: search criteria ...... Select: return result list var result2 = from m in master where m. level <10 // query condition orderby m. level // sort orderby m in ascending order based on level. level descending // sort select m in descending order by level; foreach (var item in result2) {Console. writeLine (item);} Console. readKey (); [search and sort by conditions] var ressult22 = from m in master. where (m => m. level <10 ). orderBy (m => m. age) // OrderBy & OrderByDescending select m in ascending and descending order; foreach (var item in ressult22) {Console. writeLine (item);} Console. readKey (); [3]: Call the function method to implement the search function var result3 = master. where (T1); foreach (var item in result3) {Console. writeLine (item);} Console. readKey (); // the class to be called static bool T1 (Master m) {if (m. level <10) {return true;} else {return false;} [fourth]: Use the lamada expression to find var result4 = master. where (m => m. level <10); // lamada expression parameter => Expression foreach (var item in result4) {Console. writeLine (item);} Console. readKey (); [type 5]: combine the two lists to form a Count (master) * Count (Kongfu) list of var result5 = from m in master from f in kongfu select new {mm = m, ff = f}; // foreach (var item in result5) {Console. writeLine (item);} Console. readKey ()
[Sixth]: query a master. level = Kongfu. id, when the two information var result6 = from m in master from f in kongfu // where m. level = f. id where m. kongfu = f. name & f. power> 90 select new {mm = m, ff = f}; foreach (var item in result6) {Console. writeLine (item);} Console. readKey ();
[7]: achieve the same attributes in the master and Kongfu. Output var result7 = from m in master // put the information in the kongfu list into k and select m. kongfu = k. join k in kongfu on m. kongfu equals k. name select new {mm = m, kk = k}; foreach (var item in result7) {Console. writeLine (item);} Console. readKey (); [eighth type]: one person practices one skill, one skill, how many people practice var result8 = from k in kongfu join m in master on k. name equals m. kongfu into groups // classify martial arts experts to see which kung fu is practiced by orderby groups. count () // select new {kongfu = k, count = groups according to the practitioner. count ()}; // groups. count: Get foreach (var item in result8) {Console. writeLine (item);} Console. readKey ();

// Put people with the same practices together and output the information of master and kongfu var result = from k in kongfu join m in master on k. name equals m. kongfu select new {mm = m, kk = k}; foreach (var item in result) {Console. writeLine (item);} Console. readKey ();
[Ninth]: grouping by the gang, var result9 = from m in master group m by m. menpai // group the sects and put them into m. into p // indicates putting them in p select new {count = p. count (), key = p. key}; // count = p. count () indicates the number of people in this group // key = p. key indicates the score, that is, the keyword "Key" is Menpai foreach (var item in result9) {Console. writeLine (item);} Console. readKey (); [10]: Determine whether the element in the sequence meets the condition Any. In the internal loop, traverse each element according to m => m. menpai determines whether the condition bool result10 = master is met. any (m => m. menpai = ""); Console. writeLine (result10); Console. readKey ();
From Weizhi note (Wiz)



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.