[英]MoreLinq - How to FullJoin without getting null values from selectors?
MoreLinq provides the FullJoin extension. MoreLinq 提供 FullJoin 扩展。 However, I want the bothSelector function (it's a parameter of the FullJoin function) to return the TResult only if its not null.
但是,我希望 bothSelector 函数(它是 FullJoin 函数的一个参数)仅在 TResult 不为空时才返回它。
Example:例子:
Given two lists full joined by the number:给定两个由数字完全连接的列表:
List 1: 1,2,3 List 2: 1,2,3,4,5列表 1:1、2、3 列表 2:1、2、3、4、5
Result List 3 : null, null, null, 4 , 5结果列表 3 : null, null, null, 4 , 5
Desired: 4, 5期望:4、5
public void X()
{
var list1 = new List<int> { 1, 2, 3, 4, 5 };
var list2 = new List<int> { 4, 5 };
list1.FullJoin(
list2,
item => item,
item1 => item1,
item2 => item2,
(item1, item2) => item1);
}
Can this be possible?这可能吗?
Thanks.谢谢。
Using original FullJoin code a little bit modified:使用原始 FullJoin 代码稍作修改:
/// <summary>
/// Full join without returning null values.
/// MoreLinq - https://github.com/morelinq/MoreLINQ/blob/master/MoreLinq/FullJoin.cs
/// </summary>
public static IEnumerable<TResult> FullJoinExceptNull<TFirst, TSecond, TKey, TResult>(
this IEnumerable<TFirst> first,
IEnumerable<TSecond> second,
Func<TFirst, TKey> firstKeySelector,
Func<TSecond, TKey> secondKeySelector,
Func<TFirst, TResult> firstSelector,
Func<TSecond, TResult> secondSelector,
Func<TFirst, TSecond, TResult> bothSelector,
IEqualityComparer<TKey> comparer = null)
{
if (first == null) throw new ArgumentNullException(nameof(first));
if (second == null) throw new ArgumentNullException(nameof(second));
if (firstKeySelector == null) throw new ArgumentNullException(nameof(firstKeySelector));
if (secondKeySelector == null) throw new ArgumentNullException(nameof(secondKeySelector));
if (firstSelector == null) throw new ArgumentNullException(nameof(firstSelector));
if (secondSelector == null) throw new ArgumentNullException(nameof(secondSelector));
if (bothSelector == null) throw new ArgumentNullException(nameof(bothSelector));
return _(); IEnumerable<TResult> _()
{
var seconds = second.Select(e => new KeyValuePair<TKey, TSecond>(secondKeySelector(e), e)).ToArray();
var secondLookup = seconds.ToLookup(e => e.Key, e => e.Value, comparer);
var firstKeys = new HashSet<TKey>(comparer);
foreach (var fe in first)
{
var key = firstKeySelector(fe);
firstKeys.Add(key);
using var se = secondLookup[key].GetEnumerator();
if (se.MoveNext())
{
do
{
var result = bothSelector(fe, se.Current);
if (result == null) continue;
yield return result;
}
while (se.MoveNext());
}
else
{
se.Dispose();
yield return firstSelector(fe);
}
}
foreach (var se in seconds)
{
if (!firstKeys.Contains(se.Key))
yield return secondSelector(se.Value);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.