Linq-1查询语言

查询语言我们了解很多,大体上他们的思路语言都是相同的,linq也不难,只是我们把它想的太复杂了而已。Linq语言集成化查询
  基础:1,泛型 2,lambda
  from 元素 in 集合
  where 元素条件
  orderby 元素.属性 ascending
  group 元素 by 元素.属性
  select 元素

看段练习吧...

//查询出集合中年龄大于25的学生

//查询出集合中年龄大于25,且以“哈”字开头的学生

using System;
using System.Linq;
using System.Collections.Generic;
 public class studyLinq
 {
	 public static void Main()
	 {
		 /*获取他们的扩展名*/
		 string file = "sdfhue.gawei.ge..gaew.html";
		 Console.WriteLine(file.GetFileType());
		 string ttt= "dfhue,ge.dfe.mp3";
		 Console.WriteLine(ttt.GetFileType());
		List<Student> list = new List<Student>();
	    list.Add(new Student(){Age=10,Name="Jack"});
		list.Add(new Student(){Age=67,Name="Mack"});
		list.Add(new Student(){Age=23,Name="Dack"});
		list.Add(new Student(){Age=26,Name="哈密瓜"});
		list.Add(new Student(){Age=8,Name="Eack"});
		list.Add(new Student(){Age=34,Name="Hack"});
		list.Add(new Student(){Age=18,Name="小红"});
		//查询出集合中年龄大于25的学生
		IEnumerable<Student> result = list.Where<Student>(p=>p.Age>25);
		foreach(Student s in result)
		{
		 Console.WriteLine(s.Age +" "+ s.Name);
		}
		//查询出集合中年龄大于25,且以“哈”字开头的学生
		IEnumerable<Student> result1 = list.Where(p => p.Age>25 &&p.Name.StartsWith("哈"));
		 foreach(Student dd in result1)
		 {

			 Console.WriteLine(dd.Name + " " + dd.Age);
          	 }
		
	 }
 
 public static void Test()
{
	int[] a  = {2,3,5,6,7,29,34,45,85,8,9,54,54,56,5};
	//var c = a.Where(p=>{return p>20});
	IEnumerable<int> c = a.Where(p=> p>20);
	foreach(int d in c)
	{
	  Console.WriteLine(d);
	}
  }
 }
public class Student
{
	public string Name
	{
	 get;
	 set;
	}
	public int Age
	{
	  get;
	  set;
	}
}

public static class ExetendMethod
{
  	public static string GetFileType(this string str)
	{
		string[] strs = str.Split('.');
		return strs[strs.Length-1];
	}
	
} 

 

posted @ 2012-08-04 14:23  妍珊  阅读(635)  评论(0编辑  收藏  举报