如何在LinQ中合并多组 [英] How to merge multi sets in LinQ

查看:82
本文介绍了如何在LinQ中合并多组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linq中有3套游戏,像这样:

I have 3 sets in Linq, like this:

struct Index 
{
    string code;
    int indexValue;
}

List<Index> reviews
List<Index> products
List<Index> pages

这些列表使用不同的代码.

我想将这些集合合并如下:

I want to merge these sets as following:

  • 在评论中夺冠
  • 获得第一产品
  • 抢先进入页面
  • 获得第二名 -...依此类推,请注意这些列表的大小不相同.
  • Take the first in reviews
  • Take the first in products
  • Take the first in pages
  • Take the second in reviews -... and so on, note that these lists are not same-size.

如何在Linq中做到这一点?

How can I do this in Linq?

等等,如果没有.NET 4.0,是否可以更改?

Wait, is there a change to do this without .NET 4.0?

非常感谢您

推荐答案

您可以使用 Zip 进行出价.

You could use Zip to do your bidding.

var trios = reviews
    .Zip(products, (r, p) => new { Review = r, Product = p })
    .Zip(pages, (rp, p) => new { rp.Review, rp.Product, Page = p });

对于.NET 3.5,可以很容易地实现Zip:但是有一些 gotcha .乔恩·斯凯特(Jon Skeet)撰写了一系列精彩的系列文章,内容涉及如何为对象操作员实现LINQ(出于教育目的),包括找到在Google代码上.

For .NET 3.5, it's possible to implement Zip quite easily: but there are a few gotcha s. Jon Skeet has a great post series on how to implement LINQ to objects operators (for educational purposes), including this post, on Zip. The source code of the whole series, edulinq, can be found on Google Code.

这篇关于如何在LinQ中合并多组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆