其实也是对网上linq--select篇的扩充
我在这里把两种语法都写,lambda,linq写法,方便大家更好的去学习,同时也增加了一些新的东西,如解释,例子的扩充。。。
今后一段时间,我会对linq一些基本写法进行例子和解释结合的方式和大家进行分享。
class
Program
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private static List<Student> GetStudents()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
List<Student> students = new List<Student>
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="YOUNG", Age=25, Language="Chinese"},
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="JESSIE", Age=21, Language="Scotland"},
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="KELLY", Age=18, Language="English"},
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="JUNE", Age=20, Language="English"},
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="ADRIAN", Age=22, Language="Italy"},
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="BRUCE", Age=17, Language="Scotland"},
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="BRANT", Age=30, Language="Germany"},
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Student
{ Name="BEN", Age=25, Language="Chinese"}
};
return students;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
List<Student> studentList = GetStudents();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Select(studentList);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void Select<T>(T studentList)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
OnlySearch(studentList);
SearchCollectChangeOtherCollect(studentList);
BuilderAnomy();
BuilderAnomyExter();
SelectLambda();
ComplexSelect();
ComplexSelectLambda();
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void OnlySearch<T>(T studentList)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("<hr>只查询合适对象集合的某个属性:<br>");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//lamb的写法
var students = RevealChangeType(studentList).Where(n=>n.Language == "Chinese").Select(n=>n.Name);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//linq的写法
//var students = from n in RevealChangeType(studentList)
// where n.Language == "Chinese"
// select n.Name;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var student in students)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(string.Format("<span class='result'>{0}</span>", student));
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static IEnumerable<Student> RevealChangeType<T>(T obj)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IEnumerable<Student> student = obj as IEnumerable<Student>;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return student;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void SearchCollectChangeOtherCollect<T>(T obj)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("<hr>从一个集合中查询,转化到另外一个集合:<br>");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//age Array
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] intArray =
{ 25, 1, 4, 3 };
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//lamb的写法
var students = intArray.SelectMany(n=>RevealChangeType(obj).Where(s=>s.Age == n));
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//linq的写法
//var students = from s in RevealChangeType(obj)
// from n in intArray
// where s.Age == n
// select s;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var student in students)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(string.Format("<span class='result'>{0}</span>", student.Name));
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void BuilderAnomy()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("<hr>构建匿名类型:<br>");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
List<Student> studentList = GetStudents();
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] intArray =
{ 3, 1, 6, 4 };
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//lamb的写法
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var stu = intArray.Select(n=>new
{ Id = n, Name = (studentList as List<Student>)[n].Name });
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//linq的写法
//var stu = from n in intArray
// select new{Id = n,Name=(studentList as List<Student>)[n].Name};
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var s in stu)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(string.Format("<div class='result'>id:{0};name:{1}</div>", s.Id, s.Name));
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void BuilderAnomyExter()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("<hr>构建匿名类型2——扩展已有类:<br>");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
List<Student> studentList = GetStudents();
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] intArray =
{ 3, 1, 6, 4 };
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//lamb的写法
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var students = intArray.Select(n=>new
{ Id = n, studentList[n].Name, studentList[n].Age });
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//linq的写法
//var students = from n in intArray
// select new {Id = n, studentList[n].Name, studentList[n].Age};
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var student in students)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(string.Format("<div class='result'>id:{0}; name:{1}; age:{2}</div>", student.Id, student.Name, student.Age));
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void SelectLambda()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("<hr>select的Lambda表达式:<br>");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
List<Student> studentList = GetStudents();
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] intArray =
{ 3, 1, 6, 4 };
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//lamb的写法
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var stu = intArray.Select(i=>new
{ Id = i, studentList[i].Name });
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//linq的写法
//var stu = from i in intArray
// select new {Id = i, studentList[i].Name};
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var student in stu)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(string.Format("<div class='result'>id:{0}; name:{1}</div>", student.Id, student.Name));
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void ComplexSelect()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("<hr>复合选择:<br>");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
List<Student> studentList = GetStudents();
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] stringArray =
{ "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis" };
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//lamb的写法
//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.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var stu = studentList.Where(sl=>sl.Age > 23).SelectMany(sl=>stringArray.Where(s=>s.Length > 8).Select(s=>new
{ name = sl.Name, book = s }));
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//linq的写法
//var stu = from sl in studentList
// where sl.Age > 23
// from s in stringArray
// where s.Length > 8
// select new {name = sl.Name, book = s};
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var student in stu)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(string.Format("<div class='result'>{0} is studying {1}</div>", student.name, student.book));
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void ComplexSelectLambda()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("<hr>多重选择的Lambda表达式:<br>");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
List<Student> studentList = GetStudents();
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] stringArray =
{ "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis" };
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//lamb的写法
//var stu = studentList.SelectMany(s=>stringArray.Select(str => s.Name + "has a book names " + str));
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//linq的写法
var stu = from sl in studentList
from s in stringArray
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
select new
{name=sl.Name,language=s};
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var s in stu)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(string.Format("<div class='result'>{0}</div>", s));
}
}
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
sealed
class
Student
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public int age;
public int Age
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return age; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ age = value; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string name;
public string Name
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return name; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ name = value; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string language;
public string Language
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return language; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ language = value; }
}
}
对此方法的补充
ComplexSelectLambda()
**********************************
但是现在(ComplexSelectLambda())输出的结果是:
<
hr
>
多重选择的Lambda表达式:
<
br
/>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
div
class
=
'
result
'
>
{name=YOUNG, language=Ajax}
</
div
>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
div
class
=
'
result
'
>
{name=YOUNG, language=Csharp}
</
div
>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
div
class
=
'
result
'
>
{name=YOUNG, language=Javascript}
</
div
>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
div
class
=
'
result
'
>
{name=YOUNG, language=Css Desige}
</
div
>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
div
class
=
'
result
'
>
{name=YOUNG, language=Asp.net}
</
div
>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
div
class
=
'
result
'
>
{name=YOUNG, language=Gis}
</
div
>
**********************************
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
//
问题:如何写才能输入这样的结果?
<
name
=
young,language
=
Ajax
>
<
name
=
JESSIE,language
=
Csharp
>
<
name
=
KELLY,language
=
Javascript
>
<
name
=
JUNE,language
=
Css Desige
>
<
name
=
ADRIAN,language
=
Asp.net
>
<
name
=
BRUCE,language
=
Gis
>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
//
代码:
private
static
void
ComplexSelectLambdaTwo()
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Console.WriteLine("<hr>多重选择的Lambda表达式:<br>");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
List<Student> studentList = GetStudents();
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] stringArray =
{ "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis" };
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var langlist = stringArray.Select(( la, i) => new
{ lang = la, index = i });
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var stulist = studentList.Select(( st, i) => new
{ name = st.Name, index = i });
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var stu2 = from la in langlist
from st in stulist
where la.index == st.index
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
select new
{name = st.name, language = la.lang};
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var s2 in stu2)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(string.Format("<name={0},language={1}>", s2.name, s2.language));
}
}