linq 学习日记-where

引用地址: http://www.cnblogs.com/young18/archive/2007/06/05/772127.html
using #region using
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
#endregion


public  partial  class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
根据条件查询数组#region 根据条件查询数组
        Response.Write(
"<hr>列出数组中字符长度大于等于6的字符串数组:<br>");
        
string[] Demo1 = "Ajax""Csharp""Javascript""Css Desige""Asp.net""Gis" };
        var books 
= from n in Demo1 
                    where n.Length 
>= 6 
                    select n;

        
foreach (var book in books){
            Response.Write(
string.Format("<span class='result'>{0}</span>", book));
        }

        
#endregion


        
where查询的Lambda表达式#region where查询的Lambda表达式
        Response.Write(
"<hr>where查询的快捷方法:<br>");
        var books2 
= Demo1.Where(book => book.Length >= 6);

        
foreach (var book in books2)
        
{
            Response.Write(
string.Format("<span class='result'>{0}</span>", book));
        }

        
#endregion

        
        
根据条件查询集合体#region 根据条件查询集合体

        Response.Write(
"<hr>列出集合中语言为Chinese的学生:<br>");
        List
<Student> Demo2 = GetStudents();
        var students1 
= from n in Demo2
                        where n.Language 
== "Chinese"
                        select n.Name;

        
foreach (var student in students1){
            Response.Write(
string.Format("<span class='result'>{0}</span>", student));
        }

        
#endregion


        
根据条件查询集合体#region 根据条件查询集合体
        Response.Write(
"<hr>列出集合中年龄大于等于20的英国或苏格兰学生:<br>");
        var students2 
= from n in Demo2
                        where (n.Language 
== "English" || n.Language == "Scotland"&& n.Age >= 20
                        select n;

        
foreach (var student in students2){
            Response.Write(
string.Format("<span class='result'>{0}</span>", student.Name));
        }

        
#endregion

    }


    
构造一个学生集合体#region 构造一个学生集合体
    
private List<Student> GetStudents()
    
{
        List
<Student> students = new List<Student> 
             
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;
    }

    
#endregion

}


学生类 #region 学生类
class Student
{
    
public int Age getset; }
    
public string Name getset; }
    
public string Language getset; }
}

#endregion

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值