LINQ技术

来源:互联网 发布:阿芙 知乎 编辑:程序博客网 时间:2024/06/02 07:17
1.什么是LINQ
linq被.net framwork3.5所支持,借助linq技术,可以使用一种类似sql的语法来查询任何形式的数据,目前支持的数据源有SQL Server,XML以及内存中的数据集合,开发人员也可以使用其提供的可展框架添加更多的数据源。
2.LINQ查询关键字
From, Select, Where, Order By, Group By ,Join Into。
例如: int[] arr =new int[]{ 8, 5, 89, 3, 56, 4, 1, 58 };  //内存中的数据集合,
            var m = from n in arr where n < 5 orderby n select n;     //select n from n in arr where n < 5 orderby n  注:VAR可代替任何类型
            foreach (var n in m)
            {
                Console.WriteLine(n);
            }
            Console.ReadKey();

3.什么是Linq to SQL查询技术
LINQ To SQL查询中所引用的对象是映射到数据库中的元素。LINQ编辑数据库主要有3中操作方式:添加, 修改 ,删除。
insert操作 1)创建一个LINQ to SQL对象  2)自定义一个数据对象 3)将状态的实体添加到LINQ对象中 4)将自定义LINQ对象添加到数据库
update delete操作 1)将状态实体更新/删除操作到LINQ对象中 2)将LINQ对象更新到数据库。
4.LINQ到Dataset
主要提供对离线数据的支持,只有在填充Dataset之后,才能使用LINQ to DataSet来查询数据。可以用于查询从一个或多个数据源合并的数据。
5.什么是LINQ to XML查询技术
LinQ to XML将XML文档置于内存中,可以查询和修改XML文档,修改之后,可以将其另存为文件,也可以将其序列化然后通过Internet发送。
例子:
public static Array SelectXmlLogInFo()
       {
           XElement xelem = XElement.Load(@"XML\xmlLog.xml"); //Debug里xmlLog.xml
          var queryXML=from xmlLog in xelem.Descendants("msg_log") where xmlLog.Element("user").Value=="xiong"
              select new {
                   用户名=xmlLog.Element("user").Value.ToUpper(),
                   时间=xmlLog.Element("logdate").Value,
                   消息=xmlLog.Element("message").Value
                         };
         return queryXML.ToArray();
        }

 public static void InsertLogInfo()
        {
            string strNow = DateTime.Now.ToString("yyyyMMddhhmmss");
            FileInfo fiXML = new FileInfo(@"XML\xmlLog.xml");  
            if (!(fiXML.Exists))
            {
                XDocument xelLog = new XDocument(        //创建xml文档 
                   new XDeclaration("1.0", "utf-8", "no"),
                   new XElement("ipmsg",
                       new XElement("msg_log",
                           new XElement("user", "Bin"),
                           new XElement("logdate", strNow),
                           new XElement("message", "一条xml linq测试")
                       )
                    )
                );
                xelLog.Save(@"XML\xmlLog.xml");

                Dictionary<string, string> dicLog = new Dictionary<string, string>();
                dicLog.Add("user", "Xiong");
                dicLog.Add("logdate", strNow);
                dicLog.Add("message", "这是一条添加记录测试");
                XElement xelem = XElement.Load(@"XML\xmlLog.xml");  //实例化XMLog 
                XElement newLog = new XElement("msg_log",       //执行linq添加(xmlLOG) 
                                      new XElement("user", (string)dicLog["user"]),
                                      new XElement("logdate", (string)dicLog["logdate"]),
                                      new XElement("message", (string)dicLog["message"])
                                  );
                xelem.Add(newLog);
                xelem.Save(@"XML\xmlLog.xml");  //保存xml 
            }
       }

public static void UpdateLogInfo()
        {
            XElement xelem = XElement.Load(@"XML\xmlLog.xml"); //Debug里xmlLog.xml
            var queryXML = from xmlLog in xelem.Descendants("msg_log")
                           where xmlLog.Element("user").Value == "Bin"   //所有名字为Bin的记录 
                           select xmlLog;
            foreach (XElement el in queryXML)
            {
                el.Element("user").Value = "LiuBin";          //开始修改 
            }
            xelem.Save(@"XML\xmlLog.xml");  //保存xml 
        }
        
public static void DeleteLogInfo()
        {
            XElement xelem = XElement.Load(@"XML\xmlLog.xml");   //实例化XMLog 
            var queryXML = from xmlLog in xelem.Descendants("msg_log")
                           where xmlLog.Element("user").Value=="LiuBin"
                           select xmlLog;
            queryXML.Remove();
            xelem.Save(@"XML\xmlLog.xml");
        }
原创粉丝点击