假设你有两个 IEnumerbale
的对象。我们怎样才能把它们合并(在合并排序......有些情况如合并),并创建一个独特的的IEnumerable
?我想这与邮编
,但在拉链两个列表的大小应该是相等的(也许你没有得到异常,但也许我们有一些数据丢失。)
另外,我尝试用Enumerable.Range(...),选择(...),但我没有得到一个可以接受的结果。
此外,我的问题是使用联盟完全不同或这一个,其实我说像合并排序合并我喜欢preserve列出顺序(其实只是想填补第一个列表有些差距)。
这很容易与循环来处理它,但我看不到任何完整的LINQ的方式。
编辑:
样品输入:
LST1 = {5,10,12}
LST2 = {7,9,16,20,25}
结果:{} 5,7,9,10,12,16,20,25
这可以用一个for循环和两个指针进行O(N + M)
但我在寻找LINQ的解决方案在 0 (N + M)
有关循环的解决方案:
VAR LST1 =新的名单,其中,INT> {5,10,12};
VAR LST2 =新的名单,其中,INT> {7,9,16,20,25};
VAR的结果=新名单,其中,INT>();
INT J = 0;
的for(int i = 0; I< lst1.Count;我++)
{
而(J< lst2.Count和放大器;&放大器; LST2 [J] LT; LST1 [I])
{
result.Add(LST2 [J]);
J ++;
}
result.Add(LST1 [I]);
}
而(J< lst2.Count)
{
result.Add(LST2 [J]);
J ++;
}
Console.WriteLine(的string.join(,,result.ToArray()));
解决方案
目前在LINQ中没有这样的方法。而且我不认为这是可能的合并现有的方法做你想要什么(如果是,那将是过于复杂)。
但实施这样的方法自己并不难:
静态的IEnumerable< T>合并< T>(这IEnumerable的< T>首先,
IEnumerable的< T>第二,
FUNC< T,T,布尔> predicate)
{
//验证ommited
使用(VAR firstEnumerator = first.GetEnumerator())
使用(VAR secondEnumerator = second.GetEnumerator())
{
布尔firstCond = firstEnumerator.MoveNext();
布尔secondCond = secondEnumerator.MoveNext();
而(firstCond&安培;&安培; secondCond)
{
如果(predicate(firstEnumerator.Current,secondEnumerator.Current))
{
收益回报firstEnumerator.Current;
firstCond = firstEnumerator.MoveNext();
}
其他
{
收益回报secondEnumerator.Current;
secondCond = secondEnumerator.MoveNext();
}
}
而(firstCond)
{
收益回报firstEnumerator.Current;
firstCond = firstEnumerator.MoveNext();
}
而(secondCond)
{
收益回报secondEnumerator.Current;
secondCond = secondEnumerator.MoveNext();
}
}
}
你可以使用这样的:
lst1.Merge(LST2,(I,J)=> I< j)条
Assume that you have two IEnumerbale
objects. How we can merge them (in some condition e.g merge in merge sort ...) and create a unique IEnumerable
? I tried this with Zip
, but in Zip the two list sizes should be equal (maybe you didn't get exception but maybe we have some data lost.)
In addition, I try it by using Enumerable.Range(...).Select(...) but i didn't get an acceptable result.
Furthermore, my question is totally different from using Union or this one, in fact as I said like merge in merge sort I like to preserve lists order (in fact just want to fill some gaps in first list).
It's easy to handle it with for loop, but i can't see any full linq way.
Edit:
Sample input:
lst1 = {5,10,12}
lst2 = {7,9,16,20,25}
result: {5,7,9,10,12,16,20,25}
this can be done with a for loop and two pointer in O(n + m)
but I'm looking for linq solution in O(n+m)
for loop solution:
var lst1 = new List<int> { 5, 10, 12 };
var lst2 = new List<int> { 7, 9, 16, 20, 25 };
var result = new List<int>();
int j = 0;
for (int i = 0; i < lst1.Count; i++)
{
while (j < lst2.Count && lst2[j] < lst1[i])
{
result.Add(lst2[j]);
j++;
}
result.Add(lst1[i]);
}
while (j < lst2.Count)
{
result.Add(lst2[j]);
j++;
}
Console.WriteLine(string.Join(",", result.ToArray()));
解决方案
There is no such method in LINQ. And I don't think it's possible to combine the existing methods to do exactly what you want (if it was, it would be overly complicated).
But implementing such method yourself isn't that hard:
static IEnumerable<T> Merge<T>(this IEnumerable<T> first,
IEnumerable<T> second,
Func<T, T, bool> predicate)
{
// validation ommited
using (var firstEnumerator = first.GetEnumerator())
using (var secondEnumerator = second.GetEnumerator())
{
bool firstCond = firstEnumerator.MoveNext();
bool secondCond = secondEnumerator.MoveNext();
while (firstCond && secondCond)
{
if (predicate(firstEnumerator.Current, secondEnumerator.Current))
{
yield return firstEnumerator.Current;
firstCond = firstEnumerator.MoveNext();
}
else
{
yield return secondEnumerator.Current;
secondCond = secondEnumerator.MoveNext();
}
}
while (firstCond)
{
yield return firstEnumerator.Current;
firstCond = firstEnumerator.MoveNext();
}
while (secondCond)
{
yield return secondEnumerator.Current;
secondCond = secondEnumerator.MoveNext();
}
}
}
And you could use it like this:
lst1.Merge(lst2, (i, j) => i < j)