关于linq的学习笔记
时间:2010-08-25 来源:xrh
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
/*定义两个泛型实体*/
IList<pro> student = new List<pro>();
student.Add(new pro() { Name = "NI", Grade = "1" });
student.Add(new pro() { Name = "wo", Grade = "2" });
student.Add(new pro() { Name = "ta", Grade = "3" });
IList<pro> school = new List<pro>();
school.Add(new pro() { Name = "wo", Grade = "2" });
school.Add(new pro() { Name = "you", Grade = "4" });
/*查出student在school中不存在的记录*/
var a = from o in student
where !(from b in school select b.Name).Contains(o.Name)
select o;
foreach (pro t in a)
{
Console.Write("{0},{1}", t.Name, t.Grade);
Console.Write("\r\n");
}
Console.Write("隔开");
/*查出符合条件的记录*/
var g = from c in student where c.Name=="wo" || c.Grade=="3" select c ;
foreach (pro v in g)
{
Console.Write("{0},{1}", v.Name, v.Grade);
Console.Write("\r\n");
}
Console.ReadLine();
}
}
/*定义学生实体类*/
public class pro
{
public string Name
{
get;
set;
}
public string Grade
{
get;
set;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
/*定义两个泛型实体*/
IList<pro> student = new List<pro>();
student.Add(new pro() { Name = "NI", Grade = "1" });
student.Add(new pro() { Name = "wo", Grade = "2" });
student.Add(new pro() { Name = "ta", Grade = "3" });
IList<pro> school = new List<pro>();
school.Add(new pro() { Name = "wo", Grade = "2" });
school.Add(new pro() { Name = "you", Grade = "4" });
/*查出student在school中不存在的记录*/
var a = from o in student
where !(from b in school select b.Name).Contains(o.Name)
select o;
foreach (pro t in a)
{
Console.Write("{0},{1}", t.Name, t.Grade);
Console.Write("\r\n");
}
Console.Write("隔开");
/*查出符合条件的记录*/
var g = from c in student where c.Name=="wo" || c.Grade=="3" select c ;
foreach (pro v in g)
{
Console.Write("{0},{1}", v.Name, v.Grade);
Console.Write("\r\n");
}
Console.ReadLine();
}
}
/*定义学生实体类*/
public class pro
{
public string Name
{
get;
set;
}
public string Grade
{
get;
set;
}
}
}
运行结果:
NI,1
ta,3
隔行wo,2
ta,3
相关阅读 更多 +