LINQ问题,求解 有一个表List<int>,希望获取某个整数i连续出现t次的次数(比如数字2连续出现3次的情况有几次),不知道用LINQ有没有办法实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 List<int> lst = new List<int>() ; var ary = Regex.Matches(string.Join(",", lst.Select(t => t.ToString()).ToArray()), @"2(,2(?=\D|$))+").Cast<Match>().Count(t => t.Value.Length - t.Value.Replace("2", "").Length == 3); 1楼的正则完全可以,给你个linq的,比较中规中矩 int[] arr = new int[] { 1, 2, 2, 2, 4, 5, 2, 2,2, 6 }; int v = arr.Where((x, index) => index<arr.Length-3&&x == 2 && arr[index] == arr[index + 1] && arr[index + 1] == arr[index + 2]).Select(i => i).Count(); 一楼的正则复制错了改为@"(?<=\D|^)2(,2(?=\D|$))+" void Main(){ int[] arr = new int[] { 1, 2, 2, 2, 4, 5, 2, 2, 2, 6 }; List<List<int>> result=new List<List<int>>{new List<int>{arr[0]}}; arr.Aggregate((x,y)=> { if(x==result.Last().Last()) { if(x==y) result.Last().Add(y); else result.Add(new List<int>{y}); } else if(x==y) result.Add(new List<int>{x,y}); return y; }); // int c=result.Where(x=>x.Count()==3 && x.First()==2).Count(); //Console.WriteLine(c); result.ForEach(x=>Console.WriteLine("{0} 出现了 {1}次",x.First(),x.Count())); /* 1 出现了 1次 2 出现了 3次 4 出现了 1次 5 出现了 1次 2 出现了 3次 6 出现了 1次 */} 我真的膜拜bdmh,第一次见这么玩Linq的,Linq也搁了很长时间了。 http://blog.csdn.net/q107770540/article/details/6625243 以上是最接近我想要的,但我希望获取连续出现t次的次数,比如int[] arr = new int[] { 1, 2, 2, 2, 4, 5, 2, 2, 2, 6, 2 }中,2连续出现3次的情况有2次。这对于LINQ来说是不是太繁琐了? 谢谢!不过最主要还是希望用LINQ求解 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { int[] arr = new int[] { 1, 2, 2, 2, 4, 5, 2, 2, 2, 6 }; int groupid = 0; var query = arr.Select((x, i) => new { x, group = (i == 0 ? groupid : (x == arr[i - 1] ? groupid : ++groupid)) }) .GroupBy(x => x.group) .Select(x => string.Format("{0}连续{1}个", x.First().x, x.Count())) .GroupBy(x => x).Select(x => x.First() + "有" + x.Count() + "次"); foreach (string item in query) { Console.WriteLine(item); } } }}1连续1个有1次2连续3个有2次4连续1个有1次5连续1个有1次6连续1个有1次Press any key to continue . . . 另外督察的结果再做一次分组也可以实现。结果和我的一样。 var query = arr.Select((x, i) => new { x, group = (i == 0 ? groupid : (x == arr[i - 1] ? groupid : ++groupid)) }) .GroupBy(x => x.group) .Select(x => string.Format("{0}连续{1}个", x.First().x, x.Count())) //.GroupBy(x => x).Select(x => x.First() + "有" + x.Count() + "次");我的程序去掉最后一行和督察的等价 谢谢!这就是我想要的!看来LINQ真是潜力无穷,要好好学习学习才行! C# dotnet动态为类添加属性 对象序列化网络传输 问一个 WPF 存储过程的问题 用UDP传送数据结构? 跪求!!我想用C#实现qq抖动窗口。给详细讲解一下代码。谢谢! 关于水晶报表(Crystal Reports)使用ado.net数据集的若干问题(高手请进) C#怎么判断Outlook是否收到新邮件 如何固定窗口的大小 急救! 仰仗各位了! 多谢! 入门级问题:获取指定文件夹中所有文件 [100分] c#简单多线程例子。 ImeMode问题
var ary = Regex.Matches(string.Join(",", lst.Select(t => t.ToString()).ToArray()), @"2(,2(?=\D|$))+").Cast<Match>().Count(t => t.Value.Length - t.Value.Replace("2", "").Length == 3);
int v = arr.Where((x, index) => index<arr.Length-3&&x == 2 && arr[index] == arr[index + 1] && arr[index + 1] == arr[index + 2]).Select(i => i).Count();
@"(?<=\D|^)2(,2(?=\D|$))+"
{
int[] arr = new int[] { 1, 2, 2, 2, 4, 5, 2, 2, 2, 6 };
List<List<int>> result=new List<List<int>>{new List<int>{arr[0]}};
arr.Aggregate((x,y)=>
{
if(x==result.Last().Last())
{
if(x==y) result.Last().Add(y);
else result.Add(new List<int>{y});
}
else if(x==y) result.Add(new List<int>{x,y});
return y;
});
// int c=result.Where(x=>x.Count()==3 && x.First()==2).Count();
//Console.WriteLine(c);
result.ForEach(x=>Console.WriteLine("{0} 出现了 {1}次",x.First(),x.Count()));
/*
1 出现了 1次
2 出现了 3次
4 出现了 1次
5 出现了 1次
2 出现了 3次
6 出现了 1次
*/
}
我真的膜拜bdmh,第一次见这么玩Linq的,Linq也搁了很长时间了。
以上是最接近我想要的,但我希望获取连续出现t次的次数,比如int[] arr = new int[] { 1, 2, 2, 2, 4, 5, 2, 2, 2, 6, 2 }中,2连续出现3次的情况有2次。
这对于LINQ来说是不是太繁琐了?
谢谢!不过最主要还是希望用LINQ求解
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 2, 2, 4, 5, 2, 2, 2, 6 };
int groupid = 0;
var query = arr.Select((x, i) => new { x, group = (i == 0 ? groupid : (x == arr[i - 1] ? groupid : ++groupid)) })
.GroupBy(x => x.group)
.Select(x => string.Format("{0}连续{1}个", x.First().x, x.Count()))
.GroupBy(x => x).Select(x => x.First() + "有" + x.Count() + "次");
foreach (string item in query)
{
Console.WriteLine(item);
}
}
}
}1连续1个有1次
2连续3个有2次
4连续1个有1次
5连续1个有1次
6连续1个有1次
Press any key to continue . . .
另外督察的结果再做一次分组也可以实现。结果和我的一样。 var query = arr.Select((x, i) => new { x, group = (i == 0 ? groupid : (x == arr[i - 1] ? groupid : ++groupid)) })
.GroupBy(x => x.group)
.Select(x => string.Format("{0}连续{1}个", x.First().x, x.Count()))
//.GroupBy(x => x).Select(x => x.First() + "有" + x.Count() + "次");我的程序去掉最后一行和督察的等价
谢谢!这就是我想要的!
看来LINQ真是潜力无穷,要好好学习学习才行!