java linq select_Linq select

class Program

{

private static List GetStudents()

{

List students = new List {

new Student{ Name="YOUNG", Age=25, Language="Chinese"},

new Student{ Name="JESSIE", Age=21, Language="Scotland"},

new Student{ Name="KELLY", Age=18, Language="English"},

new Student{ Name="JUNE", Age=20, Language="English"},

new Student{ Name="ADRIAN", Age=22, Language="Italy"},

new Student{ Name="BRUCE", Age=17, Language="Scotland"},

new Student{ Name="BRANT", Age=30, Language="Germany"},

new Student{ Name="BEN", Age=25, Language="Chinese"}

};

return students;

}

static void Main(string[] args)

{

List studentList = GetStudents();

Select(studentList);

}

private static void Select (T studentList)

{

OnlySearch(studentList);

SearchCollectChangeOtherCollect(studentList);

BuilderAnomy();

BuilderAnomyExter();

SelectLambda();

ComplexSelect();

ComplexSelectLambda();

}

private static void OnlySearch (T studentList)

{

Console.WriteLine("


只查询合适对象集合的某个属性:
");

//lamb的写法

var students = RevealChangeType(studentList).Where(n=>n.Language == "Chinese").Select(n=>n.Name);

//linq的写法

//var students = from n in RevealChangeType(studentList)

//              where n.Language == "Chinese"

//              select n.Name;

foreach (var student in students)

{

Console.WriteLine(string.Format(" {0} ", student));

}

}

private static IEnumerable RevealChangeType (T obj)

{

IEnumerable student = obj as IEnumerable ;

return student;

}

private static void SearchCollectChangeOtherCollect (T obj)

{

Console.WriteLine("


从一个集合中查询,转化到另外一个集合:
");

//age Array

int[] intArray ={ 25, 1, 4, 3 };

//lamb的写法

var students = intArray.SelectMany(n=>RevealChangeType(obj).Where(s=>s.Age == n));

//linq的写法

//var students = from s in RevealChangeType(obj)

//              from n in intArray

//              where s.Age == n

//              select s;

foreach (var student in students)

{

Console.WriteLine(string.Format(" {0} ", student.Name));

}

}

private static void BuilderAnomy()

{

Console.WriteLine("


构建匿名类型:
");

List studentList = GetStudents();

int[] intArray = { 3, 1, 6, 4 };

//lamb的写法

var stu = intArray.Select(n=>new { Id = n, Name = (studentList as List )[n].Name });

//linq的写法

//var stu = from n in intArray

//          select  new{Id = n,Name=(studentList as List )[n].Name};

foreach (var s in stu)

{

Console.WriteLine(string.Format("

id:{0};name:{1}
", s.Id, s.Name));

}

}

private static void BuilderAnomyExter()

{

Console.WriteLine("


构建匿名类型2——扩展已有类:
");

List studentList = GetStudents();

int[] intArray = { 3, 1, 6, 4 };

//lamb的写法

var students = intArray.Select(n=>new { Id = n, studentList[n].Name, studentList[n].Age });

//linq的写法

//var students = from n in intArray

//              select new {Id = n, studentList[n].Name, studentList[n].Age};

foreach (var student in students)

{

Console.WriteLine(string.Format("

id:{0}; name:{1}; age:{2}
", student.Id, student.Name, student.Age));

}

}

private static void SelectLambda()

{

Console.WriteLine("


select的Lambda表达式:
");

List studentList = GetStudents();

int[] intArray = { 3, 1, 6, 4 };

//lamb的写法

var stu = intArray.Select(i=>new { Id = i, studentList[i].Name });

//linq的写法

//var stu = from i in intArray

//          select new {Id = i, studentList[i].Name};

foreach (var student in stu)

{

Console.WriteLine(string.Format("

id:{0}; name:{1}
", student.Id, student.Name));

}

}

private static void ComplexSelect()

{

Console.WriteLine("


复合选择:
");

List studentList = GetStudents();

string[] stringArray = { "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis" };

//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 }));

var stu = studentList.Where(sl=>sl.Age > 23).SelectMany(sl=>stringArray.Where(s=>s.Length > 8).Select(s=>new { name = sl.Name, book = s }));

//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};

foreach (var student in stu)

{

Console.WriteLine(string.Format("

{0} is studying {1}
", student.name, student.book));

}

}

private static void ComplexSelectLambda()

{

Console.WriteLine("


多重选择的Lambda表达式:
");

List studentList = GetStudents();

string[] stringArray = { "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis" };

//lamb的写法

//var stu = studentList.SelectMany(s=>stringArray.Select(str => s.Name + "has a book names " + str));

//linq的写法

var stu = from sl in studentList

from s in stringArray

select new {name=sl.Name,language=s};

foreach (var s in stu)

{

Console.WriteLine(string.Format("

{0}
", s));

}

}

}

sealed class Student

{

public int age;

public int Age

{

get { return age; }

set { age = value; }

}

private string name;

public string Name

{

get { return name; }

set { name = value; }

}

private string language;

public string Language

{

get { return language; }

set { language = value; }

}

}

------ select top * from table 语法补充--------

skip(index).Take(count)     varResults=fromdtinctx.Customers.Skip(0).Take(10)

1.Top 查询----skip(index).Take(count)

var Results = from dt in ctx.Customers.Skip(0).Take(10)

select new

...{

dt.CustomerID,

dt.CompanyName,

dt.City

};

2.Like 查询----startwith()

from dt in ctx.Customers

where dt.ID.Startwith('A')

3.In查询----Contain()

string[] s = ...{ "ivan","aaa","aaaa"};

var Results = from dt in ctx.Customers.Skip(0).Take(10)

where s.Contains(dt.CustomerID)

select new

...{

dt.CustomerID,

dt.CompanyName,

dt.City

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值