насчет:
public static IEnumerable<T> ConcatN<T>(this IEnumerable<T> source, IEnumerable<T> source2, int maxSize)
{
if (maxSize == 0)
throw new ArgumentException(
"maxSize cannot be zero", "maxSize");
var sourceCount = source.Count();
if (sourceCount > maxSize)
throw new ArgumentException(
"source has more elements than the given maxSize");
if (sourceCount + source2.Count() < maxSize)
throw new ArgumentOutOfRangeException(
"source and source2 has combined, more elements than the given maxSize");
return source.Concat(source2.Take(maxSize - sourceCount));
}
- Если количество элементов в
source
превышает заданный maxSize
, исключение throwen.
- Если количество единиц в
source
+ source2
меньше, чем указано maxSize
, исключение - кин.
Использование:
var number1 = Enumerable.Range(0, 10);
var number2 = Enumerable.Range(100, 10);
var number12 = number1.ConcatN(number2, 15);
Console.WriteLine("Length = {0} | Sum = {1}",
number12.Count(), number12.Sum());
// Output: "Length = 15 | Sum = 555"
Где вы получаете N элементов из? –
Сделать простой цикл? – poke