Join two lists of different length
This works, even if I am not sure why you need it as linq expression:
var mix = Enumerable
.Range(0, Math.Max(listA.Count, listB.Count))
.Select(i => new[] { listA[i % listA.Count], listB[i % listB.Count] })
.SelectMany(x => x);
Join 2 lists have different length by in LINQ
This should work:
var a = new int?[] { 1, 2, 3, 4 };
var b = new int?[] { 5, 6, 7 };
var result = Enumerable.Range(0, Math.Max(a.Count(), b.Count()))
.Select(n => new[] {a.ElementAtOrDefault(n), b.ElementAtOrDefault(n)});
Do note the ?
in the array declarations. That is necessary in order to have null values in the resulting list. Omitting the ?
causes the result to have 0 instead of null
.
If you can't or don't want to declare the arrays as int?
, then you'll have to do the cast in the Select like so:
var result = Enumerable.Range(0, Math.Max(a.Count(), b.Count()))
.Select(n => new[] { a.Select(i => (int?)i).ElementAtOrDefault(n), b.Select(i => (int?)i).ElementAtOrDefault(n) });
This second bit of code will work correctly with regular int
arrays or Lists.
How to Zip two Lists of different size to create a new list that is same as the size of the longest amongst the original lists?
You can use Concat
to make them both the same size, and then zip it:
var zipped = list1.Concat(Enumerable.Repeat(0,Math.Max(list2.Count-list1.Count,0)))
.Zip(list2.Concat(Enumerable.Repeat(0,Math.Max(list1.Count-list2.Count,0))),
(a,b)=>(a,b));
Or create an extension method:
public static class ZipExtension{
public static IEnumerable<TResult> Zip<TFirst,TSecond,TResult>(
this IEnumerable<TFirst> first,
IEnumerable<TSecond> second,
Func<TFirst,TSecond,TResult> func,
TFirst padder1,
TSecond padder2)
{
var firstExp = first.Concat(
Enumerable.Repeat(
padder1,
Math.Max(second.Count()-first.Count(),0)
)
);
var secExp = second.Concat(
Enumerable.Repeat(
padder2,
Math.Max(first.Count()-second.Count(),0)
)
);
return firstExp.Zip(secExp, (a,b) => func(a,b));
}
}
So you can use like this:
//last 2 arguments are the padder values for list1 and list2
var zipped = list1.Zip(list2, (a,b) => (a,b), 0, 0);
Merge different length arrays without losing a value using Zip()
You can easily write your own LINQ-like extension method that will do it:
public static class MyEnumerable
{
public static IEnumerable<TResult> ZipWithDefault<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
{
bool firstMoveNext, secondMoveNext;
using (var enum1 = first.GetEnumerator())
using (var enum2 = second.GetEnumerator())
{
while ((firstMoveNext = enum1.MoveNext()) & (secondMoveNext = enum2.MoveNext()))
yield return selector(enum1.Current, enum2.Current);
if (firstMoveNext && !secondMoveNext)
{
yield return selector(enum1.Current, default(TSecond));
while (enum1.MoveNext())
{
yield return selector(enum1.Current, default(TSecond));
}
}
else if (!firstMoveNext && secondMoveNext)
{
yield return selector(default(TFirst), enum2.Current);
while (enum2.MoveNext())
{
yield return selector(default(TFirst), enum2.Current);
}
}
}
}
}
But if your source is always a pair of arrays, it might be easier to simply use for
loop:
public static IEnumerable<TResult> ZipWithDefault<TFirst, TSecond, TResult>(this TFirst[] first, TSecond[] second, Func<TFirst, TSecond, TResult> selector)
{
var maxLength = Math.Max(first.Length, second.Length);
for(var i = 0; i < maxLength; i++)
{
var firstItem = i < first.Length ? first[i] : default(TFirst);
var secondItem = i < second.Length ? second[i] : default(TSecond);
yield return selector(firstItem, secondItem);
}
}
How to use foreach for two lists with different length?
int countelev = 0;
This statement needs to be the first statement of the block that you have pasted. Means the statement needs to be outside the nested foreach statements.
Related Topics
Badimageformatexception When Loading 32 Bit Dll, Target Is X86
An Object Reference Is Required for the Non-Static Field, Method, or Property
Using Lambda Expressions for Event Handlers
Iis Wcf Service Hosting VS Windows Service
.Net, Event Every Minute (On the Minute). Is a Timer the Best Option
C# Native Host with Chrome Native Messaging
How to Communicate with a Windows Service
How to Display Items in Canvas Through Binding
How to Find Reason for Generic Gdi+ Error When Saving an Image
Ef Code First - How to Set Identity Seed
C#: How to Get an Object by the Name Stored in String
How to Validate Domain Credentials
Post Build Event Execute Powershell
Compare Two Lists for Differences
Async Implementation of Ivalueconverter
Memory Efficiency and Performance of String.Replace .Net Framework