Join join query with Linq
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace LINQ01 {class Program {static void Main (string [] args) {// initialize martial arts master var masterList = new List <MartialArtsMaster> () {new MartialArtsMaster () {Id = 1, Name = "", Age = 18, Menpai = "", Kongfu = "", Level = 9}, new MartialArtsMaster () {Id = 2, Name = "Hong qigong", Age = 70, Menpa I = "", Kongfu = "dogy bat method", Level = 10}, new MartialArtsMaster () {Id = 3, Name = "Guo Jing", Age = 22, menpai = "", Kongfu = "", Level = 10}, new MartialArtsMaster () {Id = 4, Name = "", Age = 50, menpai = "Mingjiao", Kongfu = "", Level = 1}, new MartialArtsMaster () {Id = 5, Name = "", Age = 35, menpai = "Mingjiao", Kongfu = "", Level = 10}, new MartialArtsMaster () {Id = 6, Name = "Lin pingzhi", Age = 23, Menpai = "Huashan", Kongfu = "sunflower Collection", Level = 7}, new MartialArtsMaster () {Id = 7, Name = "Yue Bu", Age = 50, menpai = "Huashan", Kongfu = "sunflower Collection", Level = 8}, new MartialArtsMaster () {Id = 8, Name = "Ling huchong", Age = 23, menpai = "Huashan", Kongfu = "Gu JIU Jian", Level = 10}, new MartialArtsMaster () {Id = 9, Name = "Mei chaofeng", Age = 23, menpai = "Peach Blossom Island", Kongfu = "jiuyin Zhenjing", Level = 8}, new MartialArtsMaster () {Id = 10, Name = "Yellow pharmacist", Age = 23, Menpai = "Plum Blossom Island", Kongfu = "Bullet finger magic", Level = 10}, new MartialArtsMaster () {Id = 11, name = "Feng Qingyang", Age = 23, Menpai = "Huashan", Kongfu = "Gu JIU Jian", Level = 10 }}; // initialize the martial arts var kongfuList = new List <Kongfu> () {new Kongfu () {Id = 1, Name = "Dogging method", Power = 90}, new Kongfu () {Id = 2, Name = "", Power = 95}, new Kongfu () {Id = 3, Name = "", Power = 100 }, new Kongfu () {Id = 4, Name = "Dugu jiujian", Power = 100}, new K Ongfu () {Id = 5, Name = "jiuyin Zhenjing", Power = 100}, new Kongfu () {Id = 6, Name = "", power = 100 }}; // Method 1: Query all masters whose school level is greater than 8 // var res = new List <MartialArtsMaster> (); // foreach (var temp in masterList) // {// if (temp. level> 8) // {// res. add (temp); //} // Method 2: Use linq to search (expressions and methods) // var res = from m in masterList // set the query set after from // where m. level> 8 & m. menpai = "" // The condition statement can be used & or | // select m. name; // returns the m set. // Method 3: Method of Extension. where (filtering method) means that this method will pass all the members of the column and pass in the filtering method as parameters to determine the filtering method. // Var res = masterList. where (Test1); // Method 4: the lamda expression // var res = masterList is generally used directly. where (m => m. level> 8 & m. menpai = ""); // use 1: join query (expression method) // query Naster of Powe> 90 // var res = from m in masterList // from k in kongfuList // where m. kongfu = k. name & k. power> 90 // select new {master = m, kongfu = k}; // use 2: Joint query (extended method) // Description: m => kongfuList is masterList. selectMany another query object, // (m, k) => new {master = m, kongfu = k} is to use the lamda expression to create a new {master = m, Kongfu = k} set for two objects to be queried, here is a collection of two query objects (from m in masterList from k in kongfuList) // Where (x => x. master. kongfu = x. kongfu. name); is the statement // var res = masterList. selectist (m => kongfuList, (m, k) => new {master = m, Kongfu = k}); var res = masterList. selectist (m => kongfuList, (m, k) => new {master = m, Kongfu = k }). where (x => x. master. kongfu = x. kongfu. name & x. kongfu. power> 90); foreach (var temp in res) {Console. writeLine (temp. toString ();} Console. writeLine (res. count (); Console. readKey ();} // filter method static bool Test1 (MartialArtsMaster master) {if (master. level> 8 & master. menpai = "") return true; return false ;}}}