Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MoreLinq-如何在不从选择器获取空值的情况下进行FullJoin?_C#_Morelinq - Fatal编程技术网

C# MoreLinq-如何在不从选择器获取空值的情况下进行FullJoin?

C# MoreLinq-如何在不从选择器获取空值的情况下进行FullJoin?,c#,morelinq,C#,Morelinq,MoreLinq提供了FullJoin扩展。但是,我希望bothSelector函数(它是FullJoin函数的一个参数)仅在其不为null时返回TResult 例如: 给定两个由数字完全连接的列表: 清单1:1,2,3 清单2:1,2,3,4,5 结果列表3:null,null,null,4,5 期望值:4,5 public void X() { var list1 = new List<int> { 1, 2, 3, 4, 5 };

MoreLinq提供了FullJoin扩展。但是,我希望bothSelector函数(它是FullJoin函数的一个参数)仅在其不为null时返回TResult

例如:

给定两个由数字完全连接的列表:

清单1:1,2,3 清单2:1,2,3,4,5

结果列表3:null,null,null,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);


    }
public void X()
{
var list1=新列表{1,2,3,4,5};
var list2=新列表{4,5};
列表1.FullJoin(
清单2,
项目=>项目,
item1=>item1,
item2=>item2,
(第1项、第2项)=>第1项;
}
这可能吗


谢谢。

使用原始的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);
            }
        }
    }
//
///不返回空值的完全联接。
///MoreLinq-https://github.com/morelinq/MoreLINQ/blob/master/MoreLinq/FullJoin.cs
/// 
公共静态IEnumerable FullJoinExceptNull(
这是第一次,
我数不清的秒,
Func firstKeySelector,
Func secondKeySelector,
Func firstSelector,
Func第二选择器,
Func bothSelector,
IEqualityComparer比较器=空)
{
如果(first==null)抛出新的ArgumentNullException(nameof(first));
如果(second==null)抛出新的ArgumentNullException(nameof(second));
如果(firstKeySelector==null)抛出新的ArgumentNullException(nameof(firstKeySelector));
如果(secondKeySelector==null)抛出新的ArgumentNullException(nameof(secondKeySelector));
如果(firstSelector==null)抛出新的ArgumentNullException(nameof(firstSelector));
如果(secondSelector==null)抛出新的ArgumentNullException(nameof(secondSelector));
如果(bothSelector==null)抛出新的ArgumentNullException(nameof(bothSelector));
返回();IEnumerable())
{
var seconds=second.Select(e=>newkeyvaluepair(secondKeySelector(e),e)).ToArray();
var secondLookup=seconds.ToLookup(e=>e.Key,e=>e.Value,comparer);
var firstKeys=新哈希集(比较器);
foreach(第一个变量为fe)
{
var键=第一键选择器(fe);
firstKeys.Add(key);
使用var se=secondLookup[key].GetEnumerator();
if(se.MoveNext())
{
做
{
var结果=两个选择器(铁、硒电流);
如果(结果==null)继续;
收益结果;
}
while(se.MoveNext());
}
其他的
{
se.Dispose();
收益率选择器(fe);
}
}
foreach(以秒为单位的var se)
{
如果(!firstKeys.Contains(se.Key))
收益率返回第二选择器(se.值);
}
}
}

使用经过少许修改的原始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);
            }
        }
    }
//
///不返回空值的完全联接。
///MoreLinq-https://github.com/morelinq/MoreLINQ/blob/master/MoreLinq/FullJoin.cs
/// 
公共静态IEnumerable FullJoinExceptNull(
这是第一次,
我数不清的秒,
Func firstKeySelector,
Func secondKeySelector,
Func firstSelector,
Func第二选择器,
Func bothSelector,
IEqualityComparer比较器=空)
{
如果(first==null)抛出新的ArgumentNullException(nameof(first));
如果(second==null)抛出新的ArgumentNullException(nameof(second));
如果(firstKeySelector==null)抛出新的ArgumentNullException(nameof(firstKeySelector));
如果(secondKeySelector==null)抛出新的ArgumentNullException(nameof(secondKeySelector));
如果(firstSelector==null)抛出新的ArgumentNullException(nameof(firstSelector));
如果(secondSelector==null)抛出新的ArgumentNullException(nameof(secondSelector));
如果(bothSelector==null)抛出新的ArgumentNullException(nameof(bothSelector));
返回();IEnumerable())
{
var seconds=second.Select(e=>newkeyvaluepair(secondKeySelector(e),e)).ToArray();
var secondLookup=seconds.ToLookup(e=>e.Key,e=>e.Value,comparer);
var firstKeys=新哈希集(比较器);
foreach(第一个变量为fe)
{
var键=第一键选择器(fe);
firstKeys.Add(key);
使用var se=secondLookup[key].GetEnumerator();
if(se.MoveNext())
{
做
{
var结果=两个选择器(铁、硒电流);
如果(结果==null)继续;
收益结果;
}
while(se.MoveNext());
}
其他的
{
se.Dispose();
收益率选择器(fe);
}
}
foreach(以秒为单位的var se)
{
如果(!firstKeys.Contains(se.Key))
收益率返回第二选择器(se.值);
}
}
}

我们可以看看您的代码吗?可以。我编辑了这个问题。我不认为你可以从
int
列表开始得到
null
,但是假设你有对象在那里,你最好还是做
。在那里(x=>x!=null)
之后呢?让我们想象一下它的int?。。。对where的调用意味着另一次迭代,对吗?完全联接已经执行了一个或两个。。。我想这样做。我们能看看你的代码吗?是的。我编辑了这个问题。我认为你不能用
int
列表来获得
null