投影

□ 遍历数组索引,Select获取

            int[] indexes = {0, 2}; 
            string[] strs = {"a", "b", "c", "d"};
 
            var result = from i in indexes 
                select strs[i];
 
            foreach (string str in result) 
            { 
                Console.Write(str + " "); 
            } 
            Console.ReadKey();

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

结果:a c

 

□ 使用集合索引投影匿名对象

        static void Main(string[] args) 
        { 
            int[] numbers = {0, 3, 4, 5, 7}; 
            var result = numbers.Select((n, index) => new {Num = n, IsValueEqualIndex = (n == index)}); 
            foreach (var item in result) 
            { 
                Console.WriteLine("{0}:{1}",item.Num,item.IsValueEqualIndex); 
            } 
            Console.ReadKey(); 
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

结果:

 

□ 2个集合的笛卡尔积

            int[] numberA = {0,2}; 
            int[] numberB = {1,3};
 
            var result = from a in numberA 
                from b in numberB 
                select new {a, b}; 
            foreach (var item in result) 
            { 
                Console.WriteLine("{0},{1}",item.a,item.b); 
            } 
            Console.ReadKey();

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

结果:

 

□ 笛卡尔积应用,列出所有客户的所有订单

   public class Customer 
    { 
        public int Id { get; set; } 
        public string Name { get; set; } 
        public List<Order> Orders { get; set; } 
    }
 
    public class Order 
    { 
        public int Id { get; set; } 
        public decimal Total { get; set; } 
    }
 
            Order order1 = new Order(){Id = 1,Total = 10.00m}; 
            Order order2 = new Order(){Id = 2,Total = 20.00m}; 
            Order order3 = new Order(){Id = 3,Total = 30.00m};
 
            List<Customer> customers = new List<Customer>() 
            { 
                new Customer() 
                { 
                    Id = 1, 
                    Name = "张三", 
                    Orders = new List<Order>(){order1,order2} 
                }, 
                new Customer() 
                { 
                    Id = 2, 
                Name = "李四", 
                Orders = new List<Order>() { order3 } 
                } 
            };
 
 
            var result = from c in customers 
                from o in c.Orders 
                select new {c.Id, c.Name, o.Total};
 
            foreach (var item in result) 
            { 
                Console.WriteLine("客户编号:{0},姓名:{1},订单金额:{2}", item.Id,item.Name,item.Total); 
            }
 
            Console.ReadKey();    
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

结果:

 

  分组

□ group n by 条件 into g, g.Key就是分组标准和条件, g代表分组里所有元素的集合

            int[] numbers = { 5, 4, 1, 3, 9, 8, 6}; 
            var result = from n in numbers 
                group n by n%5 into g 
                select new {Remainder = g.Key, Numbers = g};
 
            foreach (var item in result) 
            { 
                Console.WriteLine("元素除以5的余数为:{0}",item.Remainder); 
                foreach (var n in item.Numbers) 
                { 
                    Console.WriteLine(n); 
                } 
            } 
            Console.ReadKey();

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

结果:

 

  连接

□ 根据2个集合的相同部分连接

    public class Product 
    { 
        public int Id { get; set; } 
        public string Name { get; set; } 
        public string Category { get; set; } 
    }
 
            List<Product> products = new List<Product>() 
            { 
                new Product(){Id=1,Name = "体恤衫",Category = "服饰"}, 
                new Product(){Id = 2,Name = "香水",Category = "化妆品"} 
            };
 
            string[] categoreis = new string[] 
            { 
                "服饰", 
                "化妆品", 
                "电子" 
            };
 
            var result = from c in categoreis 
                join p in products on c equals p.Category 
                select new {Category = c, p.Name}; 
            foreach (var item in result) 
            { 
                Console.WriteLine(item.Name + "属于:" + item.Category); 
            } 
            
            Console.ReadKey();   

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; } 

结果:

 

□ 把其中一个集合的所有元素放到一个别名集合中

            List<Product> products = new List<Product>() 
            { 
                new Product(){Id=1,Name = "体恤衫",Category = "服饰"}, 
                new Product(){Id = 2,Name = "香水",Category = "化妆品"} 
            };
 
            string[] categoreis = new string[] 
            { 
                "服饰", 
                "化妆品", 
                "电子" 
            };
 
            var result = from c in categoreis 
                join p in products on c equals p.Category into ps 
                select new {Category = c, Products = ps}; 
            foreach (var item in result) 
            { 
                Console.WriteLine("类别:{0}包括:",item.Category); 
                foreach (var p in item.Products) 
                { 
                    Console.WriteLine("      " + p.Name); 
                } 
            } 
            
            Console.ReadKey();

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

结果:

25LINQ拾遗及实例的更多相关文章

  1. 最近学习工作流 推荐一个activiti 的教程文档

    全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...

  2. Redis命令拾遗四(集合类型)—包含简单搜索筛选商品设计实例。

    本文版权,归博客园和作者吴双共同所有.转载和爬虫请注明博客园蜗牛Redis系列文章地址 http://www.cnblogs.com/tdws/tag/NoSql/ Redis数据类型之集合(Set) ...

  3. 基础拾遗------redis详解

    基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...

  4. 基础拾遗------webservice详解

    基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...

  5. 《Thinking In Java第四版》拾遗

    <Thinking In Java第四版>拾遗 转自我的github(http://katsurakkkk.github.io/2016/05/Thinking-In-Java%E7%AC ...

  6. 类和对象:拾遗 - 零基础入门学习Python039

    类和对象:拾遗 让编程改变世界 Change the world by program 这节课谈的内容主要有: 组合 ...... 此处省略N多内容,具体请看视频讲解 ...... 类.类对象和实例对 ...

  7. 基础拾遗-----mongoDB操作

    基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...

  8. Django拾遗--pagination、sitemap、admin、form

    Django拾遗--pagination.sitemap.admin.form pagination 其实这个分页模块的原理就是根据设定的每页条数来分割queryset.查询结果/每页子项数目=页数 ...

  9. Vue.js基础拾遗

    本篇目录: 模版语法 插值 指令 v-bind指令 v-on指令 计算属性与侦听器 计算属性VS方法 计算属性VS侦听属性 Class与Style绑定 绑定HTML Class 绑定内联样式 条件渲染 ...

随机推荐

  1. 一步步教你编写不可维护的 PHP 代码

    译者注:这是一篇很棒文章,使用有趣的叙述方式,从反面讲解了作为一个优秀的 PHP 工程师,有哪些事情是你不能做的.请注意哦,此篇文章罗列的行为,都是你要尽量避免的. 随着失业率越来越高,很多人意识到保 ...

  2. 使用EasyWechat快速开发微信公众号支付

    前期准备: 申请微信支付后, 会收到2个参数, 商户id,和商户key.注意,这2个参数,不要和微信的参数混淆.微信参数: appid, appkey, token支付参数: merchant_id( ...

  3. cetos6配置用msmtp和mutt发邮件(阿里云)

    Linux下可以直接用mail命令发送邮件,但是发件人是user@servername,如果机器没有外网的dns,其他人就无法回复.此时,有一个可以使用网络免费邮箱服务的邮件发送程序就比较重要了.ms ...

  4. 【洛谷】P2179 [NOI2012]骑行川藏

    题解 感谢小迪给我讲题啊,这题小迪写挺好的我就不写了吧 小迪的题解 代码 #include <iostream> #include <cstdio> #include < ...

  5. 【POJ】3270.Cow Sorting

    题解 用到一点群论的知识! 我们发现把操作写成一个置换后,一定是单个置换圈的内进行操作,把置换圈进行扩大的操作不优 我们有两个办法,一个是用全局最小的换进来,代替这个圈里最小的值,交换操作完成后再换出 ...

  6. CSUOJ 1040 Round-number

    Description Most of the time when rounding a given number, it is customary to round to some multiple ...

  7. 监控cpu、内存 <shell>

    获取cpu.内存结果 pid=$1 #获取进程pid echo $pid interval=1 #设置采集间隔 while true do echo $(date +"%y-%m-%d %H ...

  8. shell 变量中间有空格 如何传入整个变量

    parmfile='abc  123' RunProgram programname "${parmfile}"  -->传入abc 123 RunProgram progr ...

  9. shell rename directory

    mv can do two jobs. It can move files or directories It can rename files or directories To just rena ...

  10. [leetcode greedy]455. Assign Cookies

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...