Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ获取对集_C#_Linq - Fatal编程技术网

C# 使用LINQ获取对集

C# 使用LINQ获取对集,c#,linq,C#,Linq,当我有清单的时候 IList<int> list = new List<int>(); list.Add(100); list.Add(200); list.Add(300); list.Add(400); list.Add(500); 这将为您提供一个匿名“pair”对象数组,其A和B属性对应于pair元素 var pairs = list.Where( (e,i) => i < list.Count - 1 ) .Sele

当我有清单的时候

IList<int> list = new List<int>();
list.Add(100);
list.Add(200);
list.Add(300);
list.Add(400);
list.Add(500);

这将为您提供一个匿名“pair”对象数组,其A和B属性对应于pair元素

var pairs = list.Where( (e,i) => i < list.Count - 1 )
                .Select( (e,i) => new { A = e, B = list[i+1] }  );
var pairs=list.Where((e,i)=>inew{A=e,B=list[i+1]});

您可以使用for循环:

var pairs = new List<int[]>();
for(int i = 0; i < list.Length - 1; i++)
    pairs.Add(new [] {list[i], list[i + 1]);
编辑:您甚至可以在原始的
IEnumerable
上进行编辑,但它要丑陋得多:

var count = list.Count();
var pairs = list
    .SelectMany((n, i) => new [] { new { Index = i - 1, Value = n }, new { Index = i, Value = n } })
    .Where(ivp => ivp.Index >= 0 && ivp.Index < count - 1)    //We only want one copy of the first and last value
    .GroupBy(ivp => ivp.Index, (i, ivps) => ivps.Select(ivp => ivp.Value));
var count=list.count();
变量对=列表
.SelectMany((n,i)=>new[]{new{Index=i-1,Value=n},new{Index=i,Value=n})
.Where(ivp=>ivp.Index>=0&&ivp.Indexivp.Index,(i,ivps)=>ivps.Select(ivp=>ivp.Value));

从我头上掉下来,完全没有经过测试:

public static T Pairwise<T>(this IEnumerable<T> list)
{
    T last;
    bool firstTime = true;
    foreach(var item in list)
    {
        if(!firstTime) 
            return(Tuple.New(last, item));
        else 
            firstTime = false; 
        last = item;
    }
}
publicstatict成对(此IEnumerable列表)
{
不持久;
bool firstTime=true;
foreach(列表中的变量项)
{
如果(!第一次)
返回(Tuple.New(last,item));
其他的
第一次=错误;
最后一项=项目;
}
}
更一般的是:

    public static IEnumerable<TResult> Pairwise<TSource, TResult>(this IEnumerable<TSource> values, int count, Func<TSource[], TResult> pairCreator)
    {
        if (count < 1) throw new ArgumentOutOfRangeException("count");
        if (values == null) throw new ArgumentNullException("values");
        if (pairCreator == null) throw new ArgumentNullException("pairCreator");
        int c = 0;
        var data = new TSource[count];
        foreach (var item in values)
        {
            if (c < count)
                data[c++] = item;
            if (c == count)
            {
                yield return pairCreator(data);
                c = 0;
            }
        }
    }
公共静态IEnumerable成对(此IEnumerable值、int计数、Func pairCreator)
{
如果(计数<1)抛出新ArgumentOutOfRangeException(“计数”);
如果(value==null)抛出新的ArgumentNullException(“值”);
如果(pairCreator==null)抛出新的ArgumentNullException(“pairCreator”);
int c=0;
var数据=新的TSource[计数];
foreach(值中的var项)
{
如果(c
使用LINQ最优雅的方式:
list.Zip(list.Skip(1),Tuple.Create)

一个实际示例:此扩展方法获取一组点(
Vector2
),并生成“连接点”所需的一组线(
PathSegment

static IEnumerable<PathSegment> JoinTheDots(this IEnumerable<Vector2> dots)
{
    var segments = dots.Zip(dots.Skip(1), (a,b) => new PathSegment(a, b));
    return segments;
}
静态IEnumerable连接点(此IEnumerable点)
{
var段=dots.Zip(dots.Skip(1),(a,b)=>新路径段(a,b));
返回段;
}

以下解决方案使用zip方法。zip originalList和originalList。跳过(1)以获得所需的结果

    var adjacents =
            originalList.Zip(originalList.Skip(1),
                             (a,b) => new {N1 = a, N2 = b});
从以下位置使用
.Windowed()


请详细说明您的问题。您现在拥有的可能意味着许多事情中的一件……您想在原始
IEnumerable
上操作还是只在
IList
上操作?如果是这样,请看我的编辑。有人能解释一下这是怎么回事吗?它相当密集。@GameKyuubi
(e,i)
符号表示接受元素和元素索引的签名。
Where
子句允许序列中除最后一个元素外的所有元素。
Select
子句创建一个新的匿名对象,其中
a
元素分配给序列中的原始元素,而
B
元素分配给序列中的以下元素。也就是说,如果
e
表示每次迭代中的
i
th元素,则
B
获取
i+1
th元素。
static IEnumerable<PathSegment> JoinTheDots(this IEnumerable<Vector2> dots)
{
    var segments = dots.Zip(dots.Skip(1), (a,b) => new PathSegment(a, b));
    return segments;
}
    var adjacents =
            originalList.Zip(originalList.Skip(1),
                             (a,b) => new {N1 = a, N2 = b});
var source = new[] {100,200,300,400,500};
var result = source.Windowed(2).Select(x => Tuple.Create(x.First(),x.Last()));