找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1567|回复: 2

[原创] 练习 两个Linq表达式

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-28 10:46:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

  1.         static void Main(string[] args)
  2.         {
  3.             double[] ds = { 1.0, 0.222, 2.0, 3.01, 3.0 };
  4.             var query = from d in ds
  5.                         where d != Convert.ToInt32(d)
  6.                         select d;
  7.             var query1 = from d in ds
  8.                          where d == Convert.ToInt32(d)
  9.                          select d;
  10.             Console.WriteLine("\nDouble is:");
  11.             foreach (var d in query)
  12.             {
  13.                 Console.WriteLine(d.ToString());
  14.             }
  15.             Console.WriteLine("\nInt is: ");
  16.             foreach (var d in query1)
  17.             {
  18.                 Console.WriteLine(d.ToString());
  19.             }
  20.             Console.ReadLine();
  21.         }
复制代码

Output:

Double is:
0.222
3.01

Int is:
1
2
3
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-6-28 11:11:47 | 显示全部楼层
另外一种

  1.         static void Main(string[] args)
  2.         {
  3.             double[] ds = { 1.0, 0.222, 2.0, 3.01, 3.0 };
  4.             var query2 = ds.GroupBy(d => d == Convert.ToInt32(d));
  5.             int i = 1;
  6.             foreach (var d in query2)
  7.             {
  8.                 Console.WriteLine("Group {0} is: ", Convert .ToInt32( i));
  9.                 foreach (var d1 in d)
  10.                 {
  11.                     Console.WriteLine(d1.ToString());
  12.                 }
  13.                 i++;
  14.             }
  15.             Console.ReadLine();
  16.         }
复制代码

Group 1 is:
1
2
3
Group 2 is:
0.222
3.01

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-6-29 21:28:41 | 显示全部楼层

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace ConsoleApplication2
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             double[] ds = {2.0, 1.0, 4.0, 6.0, 9.0, 12.0};
  11.             Array.Sort(ds);
  12.             int n = Convert.ToInt32(ds[ds.Length - 1]);
  13.             List<int> nl = new List<int>();
  14.             int j = 0;
  15.             for (int i = 1; i < n  ; i++)
  16.             {
  17.                 if (j < ds.Count())
  18.                 {
  19.                     if (ds[j] != Convert.ToDouble(i))
  20.                     {
  21.                         nl.Add(i);
  22.                     }
  23.                     else
  24.                     {
  25.                         j++;
  26.                     }
  27.                 }
  28.                 else
  29.                 {
  30.                     nl.Add(i);
  31.                 }
  32.             }
  33.             for (int i = 0; i < nl.Count ; i++)
  34.             {
  35.                 Console.WriteLine(nl[i].ToString());
  36.             }
  37.             Console.ReadLine();
  38.         }
  39.     }
  40. }


3
5
7
8
10
11

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-5-22 06:22 , Processed in 0.441426 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表