【C#】Linq表达式和Lambda表达式用法对比

Linq表达式和Lambda表达式用法对比

什么是Linq表达式?什么是Lambda表达式?
前一段时间用到这个只是,在网上也没找到比较简单明了的方法,今天就整理了一下相关知识,有空了再仔细研究研究

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    public class Person
    {
        public int Age { get; set; }
        public string Name { get; set; }

    }
    class Program
    {
        public static List PersonsList()//方法返回Person类的List集合
        {
            List persons = new List();
            for (int i = 1; i < 8; i++)
            {
                //Person p = new Person() { Name = i + "人物年龄", Age = 8 - i, };
                Person p = new Person() { Name ="张"+i, Age = i, }; 
                persons.Add(p);
            }
            return persons;
        }

        static void Main(string[] args)
        {
            List persons = PersonsList();
            //lanbuda表达式
            //List persons1 = persons.Where(p => p.Age > 6).ToList();   //所有Age>6的Person的集合
            //Person per = persons.SingleOrDefault(p => p.Age == 1);  //Age=1的单个people类
            //List persons2 = persons.Where(p => p.Name.Contains("2")).ToList();   //所有Name包含年龄的Person的集合
            //foreach (Person p in persons2)
            //{
            //    Console.WriteLine(p.Name);
            //}

            //Linq表达式 与lanmuda 的对比
            //Linq
            var person01 = from t in persons
                           where t.Name.Contains("张") &&  t.Age==2
                           select t;
            //Lambda
            var person02 = persons.Where(t => t.Name == "张3");

            foreach (Person p in person02)
            {
                Console.WriteLine(p.Age);
            }
        }

    }
}



什么时候使用Linq?
通过上面的对比,好像Linq一文不值了。no,不是这样的。
比如下面几种情况我们就可以选择使用Linq:
例一:(本例适用于Linq to Object 和 没有建主外键的EF查询)
Lambda中的Join需要传四个参数表达式,是不是有点晕了。。。

 

var list1 = new Dictionary { { "1", "张三" }, { "2", "李四" }, { "3", "张三" }, { "4", "张三" } };
var list2 = new Dictionary { { "1", "张三" }, { "2", "李四" }, { "3", "李四" }, { "4", "张三" } };

//Linq
var obj1 = from l1 in list1
join l2 in list2
on l1.Key equals l2.Key
select new { l1, l2 };
//Lambda
var obj = list1.Join(list2, l1 => l1.Key, l2 => l2.Key, (l1, l2) => new { l1, l2 });

例二:
Lambda需要区分OrderBy、ThenBy有没有觉得麻烦

//Linq
var obj1 = from l1 in list1
join l2 in list2
on l1.Key equals l2.Key
orderby l1.Key, l2.Key descending
select new { l1, l2 };
//Lambda
var obj = list1.Join(list2, l1 => l1.Key, l2 => l2.Key, (l1, l2) => new { l1, l2 })
.OrderBy(li => li.l1.Key)
.ThenByDescending(li => li.l2.Key)
.Select(t => new { t.l1, t.l2 });

总觉得Linq更多的只是为了照顾那些写惯了sql的程序员。

联接查询(内联、左联、交叉联)

关于联接查询使用Linq会更合适一些这个上面已经说了。
接下来我们写内联、左联、交叉联的Linq和对应的Lambda代码。(目的:可能有些人不会,同时在这里也给自己做个备忘)
内联:

var list1 = new Dictionary { { "1", "张三" }, { "2", "李四" }, { "3", "张三" }, { "4", "张三" } };
var list2 = new Dictionary { { "1", "张三" }, { "2", "李四" }, { "3", "李四" }, { "4", "张三" } };
//Linq查询
var ojb2 = (from l1 in list1
join l2 in list2
on l1.Key equals l2.Key
select new { l1, l2 }).ToList();
//Lambda查询
var obj = list1.Join(list2, l1 => l1.Key, l2 => l2.Key, (l1, l2) => new { l1, l2 }).ToList();

左联:

var list1 = new Dictionary { { "1", "张三" }, { "2", "李四" }, { "3", "张三" }, { "4", "张三" } };
var list2 = new Dictionary { { "1", "张三" }, { "2", "李四" }, { "3", "李四" }, { "4", "张三" } };
//Linq查询
var ojb2 = (from l1 in list1
join l2 in list2
on l1.Key equals l2.Key into list
from l2 in list.DefaultIfEmpty()
select new { l1, l2 }).ToList();
//Lambda查询
var obj = list1.GroupJoin(list2, l1 => l1.Key, l2 => l2.Key, (l1, l2) => new { l1, l2=l2.FirstOrDefault() }).ToList();

交叉联:

var list1 = new Dictionary { { "1", "张三" }, { "2", "李四" }, { "3", "张三" }, { "4", "张三" } };
var list2 = new Dictionary { { "1", "张三" }, { "2", "李四" }, { "3", "李四" }, { "4", "张三" } };
//Linq查询
var ojb2 = (from l1 in list1
from l2 in list2
select new { l1, l2 }).ToList();
//Lambda查询
var obj = list1.SelectMany(l1 => list2.Select(l2 => new { l1,l2})).ToList();

参考:FROM :http://www.cnblogs.com/zhaopei/p/5746414.html

参考:https://blog.csdn.net/u010926964/article/details/46240215

你可能感兴趣的:(C#)