score:2
Accepted answer
This would work and returns the expected output:
public static IEnumerable<T> MergeWithRatio<T>(this IEnumerable<T> source, IEnumerable<T> mergeSequence, int ratio)
{
int currentRatio = 1;
bool mergeSequenceIsEmpty = !mergeSequence.Any();
using (var mergeEnumerator = mergeSequence.GetEnumerator())
{
foreach (var item in source)
{
yield return item;
currentRatio++;
if (currentRatio == ratio && !mergeSequenceIsEmpty)
{
if (!mergeEnumerator.MoveNext())
{
mergeEnumerator.Reset();
mergeEnumerator.MoveNext();
}
yield return mergeEnumerator.Current;
currentRatio = 1;
}
}
}
}
score:0
public static IEnumerable<T> MergeWithRatio<T>(this IEnumerable<T> source, IEnumerable<T> mergeSequence, int ratio)
{
using (IEnumerator<T> sourceEnumerator = source.GetEnumerator(),
mergeSequenceEnumerator = mergeSequence.GetEnumerator())
{
int i = 1;
while (sourceEnumerator.MoveNext())
{
yield return sourceEnumerator.Current;
i++;
if (i == ratio)
{
if (!mergeSequenceEnumerator.MoveNext())
{
mergeSequenceEnumerator.Reset();
mergeSequenceEnumerator.MoveNext();
}
yield return mergeSequenceEnumerator.Current;
i = 1;
}
}
}
}
Plus some checks on mergeSequenceEnumerator.MoveNext()
, ratio
input value`, etc.
score:1
Here is my implementation:
public static IEnumerable<T> MergeWithRatio<T>(this IEnumerable<T> source, IEnumerable<T> mergeSequence, int ratio)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (mergeSequence == null)
{
throw new ArgumentNullException("mergeSequence");
}
if (ratio <= 1)
{
throw new ArgumentOutOfRangeException("ratio must be greater one.");
}
return MergeWithRatioImpl(source, mergeSequence, ratio);
}
private static IEnumerable<T> MergeWithRatioImpl<T>(this IEnumerable<T> source, IEnumerable<T> mergeSequence, int ratio)
{
bool mergeSequenceContainsElements = true;
int i = 1;
ratio--;
using (var sourceEnumerator = source.GetEnumerator())
using (var mergeSequenceEnumerator = mergeSequence.GetEnumerator())
{
while (sourceEnumerator.MoveNext())
{
yield return sourceEnumerator.Current;
if (i++ % ratio == 0)
{
if (!mergeSequenceEnumerator.MoveNext())
{
// ToDo: Should we cache the current values for the case the
// enumerator can't be reset?
mergeSequenceEnumerator.Reset();
mergeSequenceContainsElements = mergeSequenceEnumerator.MoveNext();
}
if (mergeSequenceContainsElements)
{
yield return mergeSequenceEnumerator.Current;
}
}
}
}
}
Source: stackoverflow.com
Related Query
- Help to implement a ZipWithRatio extention method
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- How to implement left join in JOIN Extension method
- Writing an extension method to help with querying many-to-many relationships
- Help Understanding Enumerable.Join Method
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Gets exceptions ""at least one object must implement icomparable"" only when apply method ToList() to IQueryable<>
- LINQ WHERE method alters source collection
- How to implement method with expression parameter c#
- LINQ Source Code Available
- How to implement FIND method of EF in Unit Test?
- .NET 4 Code Contracts: "requires unproven: source != null"
- C# Linq query help removing foreach loops creating cleaner code
- Linq expression. Help mimize code
- Can I implement property in linq and don't call method twice?
- creating Linq to sqlite dbml from DbLinq source code
- What is the simplest way to implement an Entity Framework 6 Add-or-Update method
- How to Implement GetEnumerator method for class that implements IEnumerable<IEnumerable<T>>
- C# - Code supposed to be unreachable when calculate value in LINQ sum method
- Need help with this basic Contains<>() extension method and Lambda expressions
- Avoiding duplicate code in Linq Select method
- How to implement generic method approach using Linq and XML
- Need help making this method (using string.Join() and LINQ) into a generic one
- How to implement a generic method in Repository to make joins in linq
- most efficient Entity Framework Code First method of flattening / projecting parent entity with specific child
- Can anyone help me in completing this generic method to update from one list to another list?
- Entity Framework Code First ToList method timing out on SQL Azure
- Is there a way to make this C# method shorter and more readable with the help of Linq?
- Weird extention method behaviour in LINQ query
- Why the extension method of where for LINQ in this code would print out a single number while it shouldn't print anything at all?
More Query from same tag
- Summing linq data by year
- Use delegate for Projection in Linq to SQL
- .Select return wrong length
- EntityFunction.DiffDays Causes This function can only be invoked from LINQ to Entities
- Entity Framework throws NotSupportedException when casting an entity to its base class in a LINQ query
- How to order items in a list of dictionaries
- How can I get descendants of descendant in Linq?
- Filter in linq with ID's in a List<int>
- combine join with String.Contains in Linq query
- Linq group by and count
- Why does AsQueryable throw a StackOverflowException?
- How to write T-SQL many-to-many with subquery in EF
- Select distinct Linq
- Merge 2 Different List with order condition in C# Linq
- How to create Json from header and detail class
- Merge Complex Object List using Union / Intersect
- Show data in Grid from returned model
- LINQ query through Azure blobs of IEnumerable(Of IListBlobItem)
- linq .Sum() not working with Short properties
- Assign aggregate result to the entity property not pulling all subquery rows
- How can I convert this console mongodb query to a C# mongo driver v2 one?
- linq - order by who has been selected
- How to join multiple conditions for EF Expressions
- Get results for sub query with matching values from a List
- How to Dynamically edit list of object linq c#
- How do I finnangle the type that linq outputs into the function's return type
- Merging two lists into one by dates
- Exclude deleted child when using EF core Include
- How do I OrderBy and ThenBy in LINQ based on user input?
- Where clause in LINQ calling an async method