zoukankan      html  css  js  c++  java
  • C#数据操作LINQ

    Linq是language integrated query的缩写,即“语言集成查询”之意。

    Linq主要包含四个组件,Linq to object、Linq to XML、Linq to Dataset和Linq to SQL。本文仅涉及前两个。

    代码示例:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    
    namespace Linq
    {
        class Program
        {
            static void Main(string[] args)
            {
                //初始化要查询的数据
                List<int> inputArray = new List<int>();
                for (int i = 0; i < 10; i++)
                {
                    inputArray.Add(i);
                }
                Console.WriteLine("使用Linq方法来对集合对象查询,查询结果为:");
                LinqQuery(inputArray);
    
                Console.WriteLine("
    
    使用Linq方法来查询xml:
    ");
                LinqToXML();
                Console.ReadKey();
            }
            //使用linq返回集合中为偶数的元素
            private static void LinqQuery(List<int> collection)
            {
                var queryResult = from item in collection
                                  where item % 2 == 0 || item == 3//组合查询条件
                                  orderby item descending//倒序排序
                                  select item;
                //输出结果
                foreach (var item in queryResult)
                {
                    Console.WriteLine(item.ToString() + "  ");
                }
            }
            //初始化xml数据
            private static string xmlstring =
                 @"<books>
                  <book>
                  <author>Jack Herrington</author>
                  <title>PHP Hacks</title>
                  <publisher>O'Reilly</publisher>
                  </book>
                  <book>
                  <author>Jack Herrington</author>
                  <title>Podcasting Hacks</title>
                  <publisher>O'Reilly</publisher>
                  </book>
                  <book>
                  <author>藏锋</author>
                  <title>深入在线工具</title>
                  <publisher>啦啦啦出版社</publisher>
                  </book>
                  </books>";
            /// <summary>
            /// 使用Linq对XML文件进行查询
            /// </summary>
            private static void LinqToXML()
            {
                //导入xml
                XElement xmlDoc = XElement.Parse(xmlstring);
    
                //创建查询,获取作者为“王小为”的元素
                var queryResult = from element in xmlDoc.Elements("book")
                                  where element.Element("author").Value == "藏锋"
                                  select element;
    
                //输出查询结果
                foreach (var item in queryResult)
                {
                    Console.WriteLine("书名:" + item.Element("title").Value + " 作者:" + item.Element("author").Value + " 出版社:" + item.Element("publisher").Value);
                }
            }
        }
    }

    执行结果:

  • 相关阅读:
    简单的签到日历
    CSS样式命名整理
    Vue 数组中更新属性值后,视图不更新,等待其他元素更新后会触发的解决办法
    linux之cp/scp命令+scp命令详解
    js 进制转换
    博主QQ
    JSP的介绍及语法详解
    通过jdbc连接MySql数据库的增删改查操作
    在JAVA中自定义连接数据库的工具类
    通过sql语句对MySql数据库的基本操作
  • 原文地址:https://www.cnblogs.com/xiefengdaxia123/p/6012215.html
Copyright © 2011-2022 走看看