日期:2014-05-20  浏览次数:20989 次

问个数据筛选问题
现在文本框中有一组数,如下所示:
005, 014, 023, 032, 041
050, 104, 114, 123, 132
140, 141, 203, 213, 223
230, 231, 232, 239, 293
302, 312, 320, 321, 322
329, 392, 401, 410, 411
500, 923, 932,999

1、如何筛选出其中两位或者三位数字相同的数?如005,050,114,223,322,411,500,999

2、如何对该文本框中的每组数据按小中大的顺序排序后,去掉重复的,再顺序显示?
  1)先将每组数据按小中大顺序排序,如005,014,023,023,014,005,014,114.。。。。。
  2)去掉中间重复的数据,变成:005,014,023,114.。。。。
  3)在按照总体大小顺序显示,即按照从000,001,002.。。998,999这样的走势排序。(这一步个人觉得如果文本框中的数据本来就排好了顺序该步就可以不用再做了)
 

------解决方案--------------------
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var data = @"005, 014, 023, 032, 041
050, 104, 114, 123, 132
140, 141, 203, 213, 223
230, 231, 232, 239, 293
302, 312, 320, 321, 322
329, 392, 401, 410, 411
500, 923, 932, 999".Replace("\r\n", ", ").Split(',').Select(x => x.Trim());
            var result1 = data.Where(x => x.Distinct().Count() == 2);
            Console.WriteLine(string.Join(", ", result1));
            var result2 = data.Distinct().OrderBy(x => x);
            Console.WriteLine(string.Join(", ", result2));
        }
    }
}

------解决方案--------------------
int[] numbers = new int[]{005, 014, 023, 032, 041,
050, 104, 114, 123, 132,
140, 141, 203, 213, 223,
230, 231, 232, 239, 293,
302, 312, 320, 321, 322,
329, 392, 401,410, 411,
500, 923, 932,999};
var query = (from i in numbers
orderby i
select i).Distinct();
foreach (int i in query)
Console.WriteLine(i.ToString().PadLeft(3,'0'));
------解决方案--------------------
var data = @"005, 014, 023, 032, 041,
050, 104, 114, 123, 132,
140, 141, 203, 213, 223,
230, 231, 232, 239, 293,
302, 312, 320, 321, 322,
329, 392, 401, 410, 411,
500, 923, 932,999".Replace("\r\n", ", ").Split(',').Select(t => t.Trim());


var source = from temp in data where temp.Trim() != "" && temp.Distinct().Count() <= 2 select new { t = temp };