在 Lambda/LINQ 中组合列表

发布于 2024-07-06 00:58:44 字数 165 浏览 7 评论 0 原文

如果我有 IEnumerable> 类型的变量,是否有一个 LINQ 语句或 lambda 表达式可以应用于它,它将组合返回 IEnumerable 的列表代码>?

If I have variable of type IEnumerable<List<string>> is there a LINQ statement or lambda expression I can apply to it which will combine the lists returning an IEnumerable<string>?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ヅ她的身影、若隐若现 2024-07-13 00:58:45

怎么样

myStrings.SelectMany(x => x)

How about

myStrings.SelectMany(x => x)
不疑不惑不回忆 2024-07-13 00:58:45

不完全是单个方法调用,但您应该能够编写

var concatenated = from list in lists from item in list select item;

Where 'lists' is your IEnumerable> and concatenated is of type IEnumerable

(从技术上讲,这是对 SelectMany 的单个方法调用 - 它看起来并不像我在开头语句中所表达的全部意思。只是想澄清一下,以防万一任何人都感到困惑或发表评论 - 我在发布后才意识到它是如何阅读的)。

Not exactly a single method call, but you should be able to write

var concatenated = from list in lists from item in list select item;

Where 'lists' is your IEnumerable<List<string>> and concatenated is of type IEnumerable<string>.

(Technically this is a single method call to SelectMany - it just doesn't look like it was all I meant by the opening statement. Just wanted to clear that up in case anyone got confused or commented - I realised after I'd posted how it could have read).

睫毛上残留的泪 2024-07-13 00:58:45

制定一个简单的方法。 不需要 LINQ:

IEnumerable<string> GetStrings(IEnumerable<List<string>> lists)
{
   foreach (List<string> list in lists)
   foreach (string item in list)
   {
     yield return item;
   }
 }

Make a simple method. No need for LINQ:

IEnumerable<string> GetStrings(IEnumerable<List<string>> lists)
{
   foreach (List<string> list in lists)
   foreach (string item in list)
   {
     yield return item;
   }
 }
自控 2024-07-13 00:58:45

使用 LINQ 表达式...

IEnumerable<string> myList = from a in (from b in myBigList
                                        select b)
                             select a;

... 效果很好。 :-)

b 将是一个 IEnumerable 并且 a 将是一个 string

Using LINQ expression...

IEnumerable<string> myList = from a in (from b in myBigList
                                        select b)
                             select a;

... works just fine. :-)

b will be an IEnumerable<string> and a will be a string.

绾颜 2024-07-13 00:58:45

这是另一个 LINQ 查询理解。

IEnumerable<string> myStrings =
  from a in mySource
  from b in a
  select b;

Here's another LINQ query comprehension.

IEnumerable<string> myStrings =
  from a in mySource
  from b in a
  select b;
瑕疵 2024-07-13 00:58:44

SelectMany - 即

        IEnumerable<List<string>> someList = ...;
        IEnumerable<string> all = someList.SelectMany(x => x);

对于 someList 中的每个项目,然后使用 lambda“x => x”来获取 IEnumerable 对于内部物品。 在这种情况下,每个“x”是List,其已经是IEnumerable

然后将它们作为连续块返回。 本质上,SelectMany 类似于(简化的):

static IEnumerable<TResult> SelectMany<TSource, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, IEnumerable<TResult>> selector) {

    foreach(TSource item in source) {
      foreach(TResult result in selector(item)) {
        yield return result;
      }
    }
}

尽管这在某种程度上被简化了。

SelectMany - i.e.

        IEnumerable<List<string>> someList = ...;
        IEnumerable<string> all = someList.SelectMany(x => x);

For each item in someList, this then uses the lambda "x => x" to get an IEnumerable<T> for the inner items. In this case, each "x" is a List<T>, which is already IEnumerable<T>.

These are then returned as a contiguous block. Essentially, SelectMany is something like (simplified):

static IEnumerable<TResult> SelectMany<TSource, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, IEnumerable<TResult>> selector) {

    foreach(TSource item in source) {
      foreach(TResult result in selector(item)) {
        yield return result;
      }
    }
}

Although that is simplified somewhat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文