Framework提供了DataContext类,它类似于ADO.NET中的SqlConnection,为实体对象和数据库提供连接的桥梁。 Linq操作的数据表中必须有主键字段。

 

linq操作的实体类对象,如何让实体对象同数据库中的表或视图建立对应关系呢?

 

建立对应关系两种方式:

1. 添加新建项→ADO.Net实体数据模型→从数据库生成 :根据数据库中已存在的表创建实体模型,

2.  添加新建项→ADO.Net实体数据模型→空模型:在创建的空模型上,设计实体模型,再根据实体模型生成sql脚本,执行后在数据库中创建相应的表。

 

以上操作完成后,会自动生成实体上下文,和对应的实体类。

 

DataContext dc = new DataContext();//创建实体上下文

(1)查询表中所有的男生:

List<Student> list = (from s in dc.Students where s.sex == true select s).ToList();

 

(2)按年龄由大到小查询学生的姓名:

List<string> list =(from s in dc.Students orderby s.birthday decending select s.name).ToList();

                                                                        s.birthday ascending

 

(3)查询成绩在第3名到第10名的学生姓氏和成绩是否及格

var sel = ( from s in dc.Students orderby s.mark

                 select new { firstname = s.name.Substring(0,1),

                              isPass = (s.mark >= 60) ? true : false }

          ).Skip(2).Take(8);

foreach(var one in sel)

{

      string fn=one.firstname;

      bool b = one.isPass;

}

         

(4)查询张三、李四的成绩:

List<double?> list = (from s in dc.Students

                        where new string[]{"张三","李四"}.Contains(s.name)

                        select s.mark ).ToList();

 

(5)查询姓张的学生:

var sel = from s in dc.Students

          where s.name.IndexOf("张")==0

          select s;

var sel = from s in dc.Students

          where s.name.Contains("张")

          select s;

 

--------------------------------------------------

Linq To XML

 

主要依赖下面三个类来操作XML数据:

XDocument:代表XML文档

XElement:代表XML元素

XAttribute:代表XML属性

 

XML文档:

<?xml version="1.0" encoding="utf-8"?>

<!--这是XML文件中的注释-->

<Root>

    <student name="郭靖"

             sex="true"

             birthday="1988-02-15"

             mark="50" />

    <student name="黄蓉"

             sex="false"

             birthday="1989-01-10"

             mark="90" />

</Root>

 

(1)创建一个XML文档

XElement root = new XElement

(

    "Root",

    new XElement("student",new XAttribute("name","郭靖"),

                           new XAttribute("sex",true),

                           new XAttribute("birthday","1988-02-15"),

                           new XAttribute("mark",50) ),

    new XElement("student"),new XAttribute("name","黄蓉"),

                            new XAttribute("sex",false),

                            new XAttribute("birthday","1989-01-10"),

                            new XAttribute("mark",90) )

);

 

XDocument doc = new XDocument

(

   new XComment("这是XML文件中的注释"),

   root

);

 

doc.Save("c:\\student.xml");     //XDocument使用Save来序列化XML文档

 

(2) 修改元素和属性

//添加一个新元素

XElement xel = new XElement("student",new XAttribute("name","张三"),

                                       new XAttribute("sex",false)

                            );

doc.Root.Add(xel);

//修改或添加属性     xel.SetAttributeValue("name","李四");

xel.SetAttributeValue("IQ","100");

 

//SetAttributeValue用于修改元素中指定属性的值,如果该属性不存在,则创建该属性。

 

(3)查询

//查询所有男生的姓名,性别

XElement root = XElement.Load("c:\\student.xml");

var sel = from s in root.Elements("student")          

           where s.Attribute("sex").Value=="true"

           select new

           {

                name=s.Attribute("name").Value,

                sex=s.Attribute("sex").Value

           };

 

//数据源为root.Element("student"),用Elements取元素中的子元素,用Attribute取元素中的属性

posted on 2013-01-15 11:56  chay  阅读(343)  评论(0编辑  收藏  举报