Linq 入门系列 select篇

其实也是对网上linq--select篇的扩充
我在这里把两种语法都写,lambda,linq写法,方便大家更好的去学习,同时也增加了一些新的东西,如解释,例子的扩充。。。

今后一段时间,我会对linq一些基本写法进行例子和解释结合的方式和大家进行分享。

None.gif   class  Program
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private static List<Student> GetStudents()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            List
<Student> students = new List<Student> dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="YOUNG", Age=25, Language="Chinese"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="JESSIE", Age=21, Language="Scotland"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="KELLY", Age=18, Language="English"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="JUNE", Age=20, Language="English"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="ADRIAN", Age=22, Language="Italy"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="BRUCE", Age=17, Language="Scotland"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="BRANT", Age=30, Language="Germany"},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new Studentdot.gif{ Name="BEN", Age=25, Language="Chinese"}
ExpandedSubBlockEnd.gif             }
;
InBlock.gif            
return students;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            List
<Student> studentList = GetStudents();
InBlock.gif
InBlock.gif            Select(studentList);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void Select<T>(T studentList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OnlySearch(studentList);
InBlock.gif            SearchCollectChangeOtherCollect(studentList);
InBlock.gif            BuilderAnomy();
InBlock.gif            BuilderAnomyExter();
InBlock.gif            SelectLambda();
InBlock.gif            ComplexSelect();
InBlock.gif            ComplexSelectLambda();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void OnlySearch<T>(T studentList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>只查询合适对象集合的某个属性:<br>");
InBlock.gif
InBlock.gif            
//lamb的写法
InBlock.gif
            var students = RevealChangeType(studentList).Where(n=>n.Language == "Chinese").Select(n=>n.Name);
InBlock.gif
InBlock.gif            
//linq的写法
InBlock.gif            
//var students = from n in RevealChangeType(studentList)
InBlock.gif            
//               where n.Language == "Chinese"
InBlock.gif            
//               select n.Name;
InBlock.gif

InBlock.gif            
foreach (var student in students)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<span class='result'>{0}</span>", student));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static IEnumerable<Student> RevealChangeType<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IEnumerable
<Student> student = obj as IEnumerable<Student>;
InBlock.gif
InBlock.gif            
return student;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void SearchCollectChangeOtherCollect<T>(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>从一个集合中查询,转化到另外一个集合:<br>");
InBlock.gif
InBlock.gif            
//age Array
ExpandedSubBlockStart.gifContractedSubBlock.gif
            int[] intArray =dot.gif25143 };
InBlock.gif
InBlock.gif            
//lamb的写法
InBlock.gif
            var students = intArray.SelectMany(n=>RevealChangeType(obj).Where(s=>s.Age == n));
InBlock.gif
InBlock.gif            
//linq的写法
InBlock.gif            
//var students = from s in RevealChangeType(obj)
InBlock.gif            
//               from n in intArray
InBlock.gif            
//               where s.Age == n
InBlock.gif            
//               select s;
InBlock.gif

InBlock.gif            
foreach (var student in students)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<span class='result'>{0}</span>", student.Name));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void BuilderAnomy()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>构建匿名类型:<br>");
InBlock.gif
InBlock.gif            List
<Student> studentList = GetStudents();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
int[] intArray = dot.gif3164 };
InBlock.gif
InBlock.gif            
//lamb的写法
ExpandedSubBlockStart.gifContractedSubBlock.gif
            var stu = intArray.Select(n=>new dot.gif{ Id = n, Name = (studentList as List<Student>)[n].Name });
InBlock.gif
InBlock.gif            
//linq的写法
InBlock.gif            
//var stu = from n in intArray
InBlock.gif            
//          select  new{Id = n,Name=(studentList as List<Student>)[n].Name};
InBlock.gif

InBlock.gif            
foreach (var s in stu)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<div class='result'>id:{0};name:{1}</div>", s.Id, s.Name));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void BuilderAnomyExter()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>构建匿名类型2——扩展已有类:<br>");
InBlock.gif
InBlock.gif            List
<Student> studentList = GetStudents();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
int[] intArray = dot.gif3164 };
InBlock.gif
InBlock.gif            
//lamb的写法
ExpandedSubBlockStart.gifContractedSubBlock.gif
            var students = intArray.Select(n=>new dot.gif{ Id = n, studentList[n].Name, studentList[n].Age });
InBlock.gif
InBlock.gif            
//linq的写法
InBlock.gif            
//var students = from n in intArray
InBlock.gif            
//               select new {Id = n, studentList[n].Name, studentList[n].Age};
InBlock.gif

InBlock.gif            
foreach (var student in students)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<div class='result'>id:{0}; name:{1}; age:{2}</div>", student.Id, student.Name, student.Age));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void SelectLambda()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>select的Lambda表达式:<br>");
InBlock.gif
InBlock.gif            List
<Student> studentList = GetStudents();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
int[] intArray = dot.gif3164 };
InBlock.gif
InBlock.gif            
//lamb的写法
ExpandedSubBlockStart.gifContractedSubBlock.gif
            var stu = intArray.Select(i=>new dot.gif{ Id = i, studentList[i].Name });
InBlock.gif
InBlock.gif            
//linq的写法
InBlock.gif            
//var stu = from i in intArray
InBlock.gif            
//          select new {Id = i, studentList[i].Name};
InBlock.gif

InBlock.gif            
foreach (var student in stu)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<div class='result'>id:{0}; name:{1}</div>", student.Id, student.Name));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void ComplexSelect()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>复合选择:<br>");
InBlock.gif
InBlock.gif            List
<Student> studentList = GetStudents();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] stringArray = dot.gif"Ajax""Csharp""Javascript""Css Desige""Asp.net""Gis" };
InBlock.gif
InBlock.gif            
//lamb的写法
InBlock.gif            
//var stu = stringArray.Where(s=>s.Length > 8).SelectMany(s=>studentList.Where(str1=>str1.Age > 23).Select(str=>new { book = s, name = str.Name }));
ExpandedSubBlockStart.gifContractedSubBlock.gif
            var stu = studentList.Where(sl=>sl.Age > 23).SelectMany(sl=>stringArray.Where(s=>s.Length > 8).Select(s=>new dot.gif{ name = sl.Name, book = s }));
InBlock.gif
InBlock.gif            
//linq的写法
InBlock.gif            
//var stu = from sl in studentList 
InBlock.gif            
//           where sl.Age > 23
InBlock.gif            
//           from s in stringArray 
InBlock.gif            
//           where s.Length > 8
InBlock.gif            
//           select new {name = sl.Name, book = s};
InBlock.gif

InBlock.gif            
foreach (var student in stu)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<div class='result'>{0} is studying {1}</div>", student.name, student.book));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void ComplexSelectLambda()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"<hr>多重选择的Lambda表达式:<br>");
InBlock.gif
InBlock.gif            List
<Student> studentList = GetStudents();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] stringArray = dot.gif"Ajax""Csharp""Javascript""Css Desige""Asp.net""Gis" };
InBlock.gif
InBlock.gif            
//lamb的写法
InBlock.gif            
//var stu = studentList.SelectMany(s=>stringArray.Select(str => s.Name + "has a book names " + str));
InBlock.gif
InBlock.gif            
//linq的写法
InBlock.gif
            var stu = from sl in studentList                     
InBlock.gif                      from s 
in stringArray                      
ExpandedSubBlockStart.gifContractedSubBlock.gif                      select 
new dot.gif{name=sl.Name,language=s};
InBlock.gif
InBlock.gif            
foreach (var s in stu)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<div class='result'>{0}</div>", s));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
sealed   class  Student
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public int age;
InBlock.gif        
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn age; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ age = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string name;
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string language;
InBlock.gif        
public string Language
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn language; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ language = value; }
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif 对此方法的补充
None.gif   ComplexSelectLambda()
None.gif
**********************************
None.gif但是现在(ComplexSelectLambda())输出的结果是:
None.gif
< hr > 多重选择的Lambda表达式: < br />
ExpandedBlockStart.gifContractedBlock.gif
< div  class = ' result ' > dot.gif {name=YOUNG, language=Ajax} </ div >
ExpandedBlockStart.gifContractedBlock.gif
< div  class = ' result ' > dot.gif {name=YOUNG, language=Csharp} </ div >
ExpandedBlockStart.gifContractedBlock.gif
< div  class = ' result ' > dot.gif {name=YOUNG, language=Javascript} </ div >
ExpandedBlockStart.gifContractedBlock.gif
< div  class = ' result ' > dot.gif {name=YOUNG, language=Css Desige} </ div >
ExpandedBlockStart.gifContractedBlock.gif
< div  class = ' result ' > dot.gif {name=YOUNG, language=Asp.net} </ div >
ExpandedBlockStart.gifContractedBlock.gif
< div  class = ' result ' > dot.gif {name=YOUNG, language=Gis} </ div >
None.gif
**********************************
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ //
None.gif 问题:如何写才能输入这样的结果?
None.gif     
None.gif
< name = young,language = Ajax >
None.gif
< name = JESSIE,language = Csharp >
None.gif
< name = KELLY,language = Javascript >
None.gif
< name = JUNE,language = Css Desige >
None.gif
< name = ADRIAN,language = Asp.net >
None.gif
< name = BRUCE,language = Gis >
ExpandedBlockStart.gifContractedBlock.gif
/**/ //
None.gif
None.gif代码:
None.gif
private   static   void  ComplexSelectLambdaTwo()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Console.WriteLine(
"<hr>多重选择的Lambda表达式:<br>");
InBlock.gif
InBlock.gif            List
<Student> studentList = GetStudents();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] stringArray = dot.gif"Ajax""Csharp""Javascript""Css Desige""Asp.net""Gis" };
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            var langlist 
= stringArray.Select(( la, i) => new dot.gif{ lang = la, index = i });
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            var stulist 
= studentList.Select(( st, i) => new dot.gif{ name = st.Name, index = i });
InBlock.gif
InBlock.gif            var stu2 
= from la in langlist
InBlock.gif                       from st 
in stulist
InBlock.gif                       where la.index 
== st.index
ExpandedSubBlockStart.gifContractedSubBlock.gif                       select 
new dot.gif{name = st.name, language = la.lang};
InBlock.gif
InBlock.gif            
foreach (var s2 in stu2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
string.Format("<name={0},language={1}>", s2.name, s2.language));
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/RuiLei/archive/2007/07/10/812978.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值