Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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# 唯一列表,但保留空行_C#_List_Linq - Fatal编程技术网

C# 唯一列表,但保留空行

C# 唯一列表,但保留空行,c#,list,linq,C#,List,Linq,我正在寻找一种使用linq制作独特列表的方法 原始列表 Level 01 Level 01 -- Blank Line -- Level 02 Level 02 -- Blank Line -- Level 03 Level 03 -- Blank Line -- Level 04 Level 04 等等 我想做的是: Level 01 -- Blank Line -- Level 02 -- Blank Line -- Level 03 -- Blank Line -- Level 04

我正在寻找一种使用linq制作独特列表的方法

原始列表

Level 01
Level 01
-- Blank Line --
Level 02
Level 02
-- Blank Line --
Level 03
Level 03
-- Blank Line --
Level 04
Level 04
等等

我想做的是:

Level 01
-- Blank Line --
Level 02
-- Blank Line --
Level 03
-- Blank Line --
Level 04

谢谢

您可以使用
可枚举。聚合

string blankLine = // whatever a blank line is

var list = new List<string>
{
    "Level 01",
    "Level 01",
    blankLine,
    "Level 02"
    "Level 02"
    blankLine,
    // ...
};

var unique = list
    .Aggregate(
        Enumerable.Empty<string>(), // Initial accumulator value
        (acc, x) => x == blankLine || !acc.Contains(x) ? acc.Append(x) : acc); 
string blankLine=//空行是什么
变量列表=新列表
{
“01级”,
“01级”,
空白行,
“02级”
“02级”
空白行,
// ...
};
var unique=list
.合计(
Enumerable.Empty(),//初始累加器值
(acc,x)=>x==空行| |!acc.Contains(x)?acc.Append(x):acc;

您的规范不完整。您希望使用此输入做什么:

Level 01
Level 02
-- Blank Line --
Level 03
Level 04
-- Blank Line --
两个连续的空白行怎么样

Level 01
Level 01
-- Blank Line --
-- Blank Line --
Level 02
如果您的输入序列为空,您想要什么?输出为空,还是仅为空行

假设你没有这个。你的要求是:

给定一个字符串的输入序列,没有两个连续的空行。取第一个字符串,跳过所有字符串,直到第一个空字符串。以空字符串为例。重复此操作直到序列结束

所以你取第一个“01级”,跳过所有行,直到空白行,你取空行,然后第一行:“02级”。跳过所有行直到下一个空行,取下下一个空行,等等

可能“空字符串”实际上是“--空行--”。我认为您足够聪明,可以相应地更改代码

你可以在一个大型LINQ声明中做到这一点。我认为如果您使用扩展方法,您的代码将更易于阅读、理解、重用、测试和维护。如果您不熟悉扩展方法,请参阅

代码相当简单:

static IEnumerable<string> SkipContiguousLines(this IEnumerable<string> lines)
{
    string blankLine = String.Empty;   // or ----- Blank Line ---- ?

    using (var lineIterator in lines.GetEnumerator())
    {
        bool lineAvailable = lineIterator.MoveNext();
        while (lineAvailable)
        {
            // there is still something in your input:
            yield return lineIterator.Current;

            // skip until blank line:
            while (lineAvailable && lineIterator.Current != blankLine)
            {
                 lineAvailable = lineIterator.MoveNext();
            }

            // if not at end, you found a blank line: return the blank line
            if (lineAvailable) yield return blankLine;
        }
    }
}
静态IEnumerable skipcontiguaouslines(此IEnumerable行)
{
string blankLine=string.Empty;//或-----空行----?
使用(line.GetEnumerator()中的var lineIterator)
{
bool lineAvailable=lineIterator.MoveNext();
while(线性可用)
{
//您的输入中仍有一些内容:
返回lineIterator.Current;
//跳到空行:
while(lineAvailable&&lineIterator.Current!=空白行)
{
lineAvailable=lineIterator.MoveNext();
}
//如果不在最后,则找到空白行:返回空白行
如果(线性可用)收益率返回空行;
}
}
}

请改进问题的格式。以目前的格式阅读是非常困难的。查看是否将行格式化为代码,它不会将换行符包装在一起。是否可能在列表后面的“Level 02”行后面有一个“Level 01”行?或者可以保证在任何给定的空行之后,在该空行之前不会有任何非空行的重复?也请看是的,我无法格式化帖子,谢谢你格式化我的帖子和链接。这不在这里。我的错误;初始值位于累加器函数之前。它仍然告诉我'IEnumerable不包含接受'IEnumerable'的第一个参数的附加定义。什么版本的.Net?它只在.Net Framework 4.7.1.Ah中引入,VS2017(.Net 4.6.1)投诉,VS2019(.Net 4.7.2)没有。而且效果很好。你假设正确,我足够聪明,只是我是个初学者,所以我还在学习。谢谢
IEnumerable<string> lines = ... // get the input
IEnumerable<string> remainingLines = lines.SkipContiguous();
var result = orderLines.GroupBy(orderLine => orderLine.OrderId)
                       .Select(group => new
                       {
                           OrderId = group.Key,
                           OrderLines = group.SkipContiguous(),
                       });
static IEnumerable<string> SkipContiguousLines(this IEnumerable<string> lines)
{
    string blankLine = String.Empty;   // or ----- Blank Line ---- ?

    using (var lineIterator in lines.GetEnumerator())
    {
        bool lineAvailable = lineIterator.MoveNext();
        while (lineAvailable)
        {
            // there is still something in your input:
            yield return lineIterator.Current;

            // skip until blank line:
            while (lineAvailable && lineIterator.Current != blankLine)
            {
                 lineAvailable = lineIterator.MoveNext();
            }

            // if not at end, you found a blank line: return the blank line
            if (lineAvailable) yield return blankLine;
        }
    }
}