# LINQ运算符，以基于通用定界符的双重列表的拆分列表[英] LINQ operator to split List of doubles to multiple list of double based on generic delimiter

### 问题描述

i具有List双重值，其中某些元素是NAN.我想转换此列表 使用NAN作为定界符列入多个双打列表. 示例:

```List<double> values =  new List<double>
{ 1.2, 2.2, 3.2, Double.NaN, 2.2, 2.3, Double.NaN, 4.1, 4.2, 4.3 };
List<List<double>> result = values.SomeLinqOperator....
```

## 推荐答案

```List<List<double>> result = values.GroupDelimited(x => x == double.NaN)
.Select(g => g.ToList())
.ToList();
```

## 其他推荐答案

```public static IEnumerable<List<T>> SplitBy<T>(
this IEnumerable<T> source, T delimiter)
{
List<T> buffer = new List<T>();
var comparer = EqualityComparer<T>.Default;
foreach (var item in source)
{
if (comparer.Equals(item, delimiter))
{
yield return buffer;
buffer = new List<T>();
}
else
{
}
}
yield return buffer;
}
static void Main()
{
List<double> values = new List<double>
{1.2,2.2,3.2,double.NaN,2.2,2.3,double.NaN,4.1,4.2,4.3 };
List<List<double>> result = values.SplitBy(double.NaN).ToList();
}
```

## 其他推荐答案

:

```List<List<double>> result = new List<List<double>>();
List<double> current = new List<double>();
foreach (double d in values)
{
if (d == double.NaN)
{
current = new List<double>();
}
else
{
}
}
```

### 问题描述

I have a List double values in which some of the elements are NaN. I want to convert this list into multiple list of doubles using NaN as delimiter. Example:

```List<double> values =  new List<double>
{ 1.2, 2.2, 3.2, Double.NaN, 2.2, 2.3, Double.NaN, 4.1, 4.2, 4.3 };
List<List<double>> result = values.SomeLinqOperator....
```

## 推荐答案

```List<List<double>> result = values.GroupDelimited(x => x == double.NaN)
.Select(g => g.ToList())
.ToList();
```

using the GroupDelimited Extension Method from here.

## 其他推荐答案

Nothing built in, but you can write one:

```public static IEnumerable<List<T>> SplitBy<T>(
this IEnumerable<T> source, T delimiter)
{
List<T> buffer = new List<T>();
var comparer = EqualityComparer<T>.Default;
foreach (var item in source)
{
if (comparer.Equals(item, delimiter))
{
yield return buffer;
buffer = new List<T>();
}
else
{
}
}
yield return buffer;
}
static void Main()
{
List<double> values = new List<double>
{1.2,2.2,3.2,double.NaN,2.2,2.3,double.NaN,4.1,4.2,4.3 };
List<List<double>> result = values.SplitBy(double.NaN).ToList();
}
```

## 其他推荐答案

This will include empty lists if you have two double.NaN values together, or one at the start or end of the input:

```List<List<double>> result = new List<List<double>>();
List<double> current = new List<double>();
foreach (double d in values)
{
if (d == double.NaN)
{