如何仅使用LINQ选择不同列表的不同长度的部分[英] How to select sections of differing lengths from list using LINQ only

本文是小编为大家收集整理的关于如何仅使用LINQ选择不同列表的不同长度的部分的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个名为"复杂"类型的列表:

List<Complex> list = new List<Complex>();
...
class Complex
{
  public string Header { get; set; }
  ...
  ...
  ...
}

列表中有一些重复的标题,这些标题表示新部分的开始.每个部分的长度可能不同,如下所示:

start 1 2 3 4 5 6 7 start 1 2 3 start 1 2 3 4 5 6 start 1 start 1 2

我想使用linq获取类型复合物的所有可能的子列表;也就是说,所有带有标头的对象,如下:

  • 1 2 3 4 5 6 7
  • 1 2 3
  • 1 2 3 4 5 6
  • 1
  • 1 2

如何仅使用linq来完成此操作?

编辑: 列表是在HTML页面解析期间创建的

<tr>
                                                <td>00:00<!--//-00:00--></td>
<td>15:00<!--//-00:00--></td>
<td class="txt_left"><a href="">News</a></td>
<td><a href="/TsV2/TV_Video_On.asp?PROG_CODE=TVCR0114">
<img src="/images/new/sub_new/icon_play01_off.gif" alt="play"  class="rollover"/></a></td>

</tr>

<tr>
                                                <td>00:15<!--//-00:00--></td>
<td>15:15<!--//-00:00--></td>
<td class="txt_left">
<a href="Tv_Pagego.asp?PROG_CODE=TVCR0556">Nice one</a></td>
<td><a href="/TV2w/TV_2Video_On.asp?PROG_CODE=TVCR0556"><img src="/images/new/sub_new/icon_play01_off.gif" alt="play"  class="rollover"/></a></td>

</tr>

推荐答案

static void Main(string[] args)
        {
            var seq = new object[] {"start", 1, 2, 3, 4, "start", 1, 2, 3, "start", 1, 2, "start", 1};

            var no = 0;

            var test = (from i in seq select new {SeqNo = (i.ToString() == "start" ? no++ : no), Item = i}).Where( x => x.Item.ToString() != "start").GroupBy( y => y.SeqNo ).Select( z => z.ToArray()).ToArray();


        }

var test = (from i in seq
                       group i by i.ToString() == "start" ? ++no : no into g
                       select g.Where( gi => gi.ToString() != "start" ).ToArray()
                       ).ToArray()
                       ;

toarray 陈述不需要

其他推荐答案

这个问题对我来说尚不清楚,但这里有一些提示:

  • 使用.distinct()获得唯一结果
  • 尝试groupby(c => c.header)
  • 使用String.split在必要时拆分字符串值

其他推荐答案

var List = new List<Complex>()
{
    new Complex() { Header = "start" },
    new Complex() { Header = "00:23" },
    new Complex() { Header = "00:33" },
    new Complex() { Header = "start" }
};

var query = List.GroupBy(x => x.Header);

这应该导致3个Igroupings一个带有" 00:23"的标题"启动",一个带有" 00:33"

更特殊的查询将返回一个iEnumerable>,其中键将成为您的标题.元素将是您的类型(复杂).

编辑:不是问题问:)

var seq = new object[] {"start", 1, 2, 3, 4, "start", 1, 2, 3, "start", 1, 2, "start", 1}.ToList();

while(seq.Count() != 0)
{
    var Output = seq.SkipWhile(x => x.ToString() != "start").Skip(1).TakeWhile(x => x.ToString() != "start");
    seq.RemoveRange(0, Output.Count() +1);
}

我喜欢...

本文地址:https://www.itbaoku.cn/post/1557159.html

问题描述

I have a List of a type named 'Complex':

List<Complex> list = new List<Complex>();
...
class Complex
{
  public string Header { get; set; }
  ...
  ...
  ...
}

There are some repeated headers in list, and these indicate the start of new sections. Each section may be of a different length, as shown below:

start 1 2 3 4 5 6 7 start 1 2 3 start 1 2 3 4 5 6 start 1 start 1 2

I would like to get all possible sublists of type Complex using LINQ; that is, all objects with headers, as follows:

  • 1 2 3 4 5 6 7
  • 1 2 3
  • 1 2 3 4 5 6
  • 1
  • 1 2

How can I accomplish this using LINQ only?

edit: List is created during html page parsing

<tr>
                                                <td>00:00<!--//-00:00--></td>
<td>15:00<!--//-00:00--></td>
<td class="txt_left"><a href="">News</a></td>
<td><a href="/TsV2/TV_Video_On.asp?PROG_CODE=TVCR0114">
<img src="/images/new/sub_new/icon_play01_off.gif" alt="play"  class="rollover"/></a></td>

</tr>

<tr>
                                                <td>00:15<!--//-00:00--></td>
<td>15:15<!--//-00:00--></td>
<td class="txt_left">
<a href="Tv_Pagego.asp?PROG_CODE=TVCR0556">Nice one</a></td>
<td><a href="/TV2w/TV_2Video_On.asp?PROG_CODE=TVCR0556"><img src="/images/new/sub_new/icon_play01_off.gif" alt="play"  class="rollover"/></a></td>

</tr>

推荐答案

static void Main(string[] args)
        {
            var seq = new object[] {"start", 1, 2, 3, 4, "start", 1, 2, 3, "start", 1, 2, "start", 1};

            var no = 0;

            var test = (from i in seq select new {SeqNo = (i.ToString() == "start" ? no++ : no), Item = i}).Where( x => x.Item.ToString() != "start").GroupBy( y => y.SeqNo ).Select( z => z.ToArray()).ToArray();


        }

or

var test = (from i in seq
                       group i by i.ToString() == "start" ? ++no : no into g
                       select g.Where( gi => gi.ToString() != "start" ).ToArray()
                       ).ToArray()
                       ;

ToArray statements are not necessery

其他推荐答案

The question is not clear for me but here are some hints:

  • Use .Distinct() to get unique results
  • Try GroupBy(c => c.Header)
  • Use string.Split to split string value if necessary

其他推荐答案

var List = new List<Complex>()
{
    new Complex() { Header = "start" },
    new Complex() { Header = "00:23" },
    new Complex() { Header = "00:33" },
    new Complex() { Header = "start" }
};

var query = List.GroupBy(x => x.Header);

This should result in 3 IGroupings one with header "Start" one with "00:23" and one with "00:33"

More specificaly the query would return an IEnumerable> where the Keys would be your header. And the elements would be your type (Complex).

EDIT: Not what the question asked it seems however this is :)

var seq = new object[] {"start", 1, 2, 3, 4, "start", 1, 2, 3, "start", 1, 2, "start", 1}.ToList();

while(seq.Count() != 0)
{
    var Output = seq.SkipWhile(x => x.ToString() != "start").Skip(1).TakeWhile(x => x.ToString() != "start");
    seq.RemoveRange(0, Output.Count() +1);
}

I like it...

相关标签/搜索