zoukankan      html  css  js  c++  java
  • 简单的LINQ例子 学习初期

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Linq.Mapping;
    using System.Data.Linq;

    namespace LQ
    {
        [Table(Name = "Customers")]      //这里是将数据库中的Customers表映射到Customer类上
        public class Customer
        {
            [Column]   //顾名思义   列
            public string CustomerID { get; set; }
            [Column]
            public string City { get; set; }

            public override string ToString()
            {
                return CustomerID + "\t" + City;
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                //NumQuery();
                ObjectQuery();
                Console.Read();
            }

            static IEnumerable<Customer> CreateCustomers()
            {
                return new List<Customer>
                {
                    new Customer{CustomerID="ALFKI",City="Berlin"},
                    new Customer{CustomerID="BONAP",City="Marseille"},
                    new Customer{CustomerID="CONSH",City="London"},
                    new Customer{CustomerID="EASTC",City="London"},
                    new Customer{CustomerID="FRANS",City="Torino"},
                    new Customer{CustomerID="LONEP",City="Portland"},
                    new Customer{CustomerID="NORTS",City="London"},
                    new Customer{CustomerID="THEBI",City="Portland"}
                };
            }

            static void ObjectQuery()
            {
                //DataContext   这个东西是LINQ最基本也是最重要的东西  是LINQ to SQL的入口点  详细的请查询MSDN
                var db = new DataContext(@"Data Source=SHNI010\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True");


                //var results = from c in CreateCustomers()
                //              where c.City == "London"
                //              select c.CustomerID;
                //LINQ的查询语句
                var results = from c in db.GetTable<Customer>()
                              where c.City == "London"
                              select c;
                db.Log=Console.Out;
                foreach (var c in results)  //循环结果集
                {
                    Console.WriteLine("{0}\t{1}",c.CustomerID,c.City);
                }
                Console.Read();
            }

        //下面这个方法是LINQ对数组的查询  可见LINQ对很多通用
            static void NumQuery()
            {
                var numbers = new int[] { 1,4,9,16,25,36};
                var evenNumbers = from p in numbers
                                  where (p % 2) == 0
                                  select p;
                Console.WriteLine("Result:");
                foreach (var val in evenNumbers)
                {
                    Console.WriteLine(val);
                }
                Console.Read();
            }
        }
    }

  • 相关阅读:
    word-spacing汉字不起作用的解决方法
    FontAwesome 4.4.0 中完整的585个图标样式CSS参考
    margin-top失效的解决方法
    今天有群友不是很清楚htm直接存数据库的危害,我简单举个例子
    前端:图文混排-怎么在不使用float的情况下实现想要的效果呢?
    前端:圆图头像制作--border-radius : 100%
    样式问题-如何一次性设置网站英文字体样式,中文字体等样式
    8.GitHub实战系列~8.使用GitHub建立自己的免费博客
    牛逼的OSQL----大数据导入
    01.SQLServer性能优化之----强大的文件组----分盘存储
  • 原文地址:https://www.cnblogs.com/Kiros/p/1920704.html
Copyright © 2011-2022 走看看