LinQ 基础

DeptEntity.cs 实体源码:

    class DeptEntity
    {
        public int DeptNo { get; set; }
        public string DName { get; set; }
        public string Loc { get; set; }


    }

    class EmpEntity
    {
        public int EmpNo { get; set; }
        public string EmpName { get; set; }
        public string Job { get; set; }
        public double Sal { get; set; }
        public DeptEntity Dept { get; set; }

    }


一、 var 关键字

VAR 是3.5新出的一个定义变量的类型
其实也就是弱化类型的定义
VAR可代替任何类型
编译器会根据上下文来判断你到底是想用什么类型的


使用var定义变量时有以下四个特点:

(1). 必须在定义时初始化。也就是必须是var s = “abcd”形式,而不能是如下形式:
var s;
s = “abcd”;

(2). 一但初始化完成,就不能再给变量赋与初始化值类型不同的值了。

(3). var要求是局部变量。

(4). 使用var定义变量和object不同,它在效率上和使用强类型方式定义变量完全一样。


var关键字用法:

1、声明匿名函数时使用var关键字

<span style="white-space:pre">	</span>    DeptEntity dept = new DeptEntity() { DeptNo = 10, DName = "销售部", Loc = "北京" };
            //1、Use the 'var' keyword to declare an anonymous function
            var obj = new { DeptNO = dept.DeptNo, DeptName = dept.DName };
            obj.DeptName.Print();  //Using the 'Print' extension method


2、LINQ投影出匿名函数结果时使用var关键字

<span style="white-space:pre">	</span>    //2. LINQ投影出匿名函数结果时使用var关键字
            var res = from item in num
                         where item % 7 == 0
                         orderby item descending
                         select item;


3、类型很明确时使用var关键字

            //3. 类型很明确时使用var关键字
            var str = "";
            var department = new List<DeptEntity>();


4、so on...


二、 扩展方法 StringHelper.cs

1 扩展方法必须定义在静态非泛型类中

2 扩展方法要是静态方法(特殊静态方法)

3 this 要加在扩展类型参数前,而且必须是第一个参数

4  扩展方法的定义与使用要在同一命名空间

//1 The extended method must be defined in a static non generic class
    static class StringHelper
    {
        //2 The extension method is a static method(special static method)
        //3 'This' must be added to the extended type parameter, and must 
        //  be the first patameter.
        //4 The definition and use of the extension method are in the same namespace
        public static string AddChar9(this string str, char ch)
        {
            return ch + str + ch;
        }

        /// <summary>
        /// Extension method 'Print', the function will be the output string
        /// </summary>
        /// <param name="str">A string to be output</param>
        public static void Print(this string str)
        {
            Console.WriteLine(str);
        }

        /// <summary>
        /// To all types of array (Array) expansion method,
       ///  the number of elements that can be output array.
        /// </summary>
        /// <param name="arr"></param>
        public static void PrintCount(this Array arr)
        {
            Console.WriteLine(arr.Length); ;
        }


三、 LinQ 基本语法

Main() 方法:

            int[] num = { 35, 21, 55, 23, 3, 14, 24 };

            #region Traditional C# syntax
            //List<int> list = new List<int>();
            //foreach (int item in num)
            //{
            //    if (item % 7 == 0)
            //    {
            //        list.Add(item);
            //    }
            //}
            //list.Sort();
            //list.Reverse();
            //foreach (int item in list)
            //{
            //    Console.WriteLine(item);
            //}
            #endregion

            #region Linq 1.Linq language + Linq function  2. Linq function  
            //1. Linq language + Linq function
            var result = from item in num
                         where item % 7 == 0
                         orderby item descending
                         select item;
            result.ToList().ForEach(item => Console.WriteLine(item));

            //2. Linq function
            var list = num.Where(item => item % 7 == 0).OrderByDescending(item => item).ToList();
            foreach (int item in list)
            {
                Console.WriteLine(item);
            }
            #endregion

            Console.ReadKey();












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值