Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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# 无法从“System.Collections.Generic.IEnumerable”转换为“System.Collections.Generic.IEnumerable”_C#_Linq - Fatal编程技术网

C# 无法从“System.Collections.Generic.IEnumerable”转换为“System.Collections.Generic.IEnumerable”

C# 无法从“System.Collections.Generic.IEnumerable”转换为“System.Collections.Generic.IEnumerable”,c#,linq,C#,Linq,我有一个错误: 无法从“System.Collections.Generic.IEnumerable”转换为“System.Collections.Generic.IEnumerable” 我尝试了许多不同的方法,但我不知道如何修复它。如果firstID为null,请尝试为其提供默认值: class first { private int? firstID; } class second { private int secondID; private int secon

我有一个错误:

无法从“System.Collections.Generic.IEnumerable”转换为“System.Collections.Generic.IEnumerable”


我尝试了许多不同的方法,但我不知道如何修复它。

如果firstID为null,请尝试为其提供默认值:

class first
{
    private int? firstID;
}

class second
{
    private int secondID;
    private int secondField;
}

public override Expression<Func<first, bool>> FirstFilter()
{
    Contex db = new Contex();
    List<second> list = (from p in db.second select p).ToList();

    return b => list.Select(p => p.secondID).Contains(b.firstID);
}

如果firstID为null,请尝试为其提供默认值:

class first
{
    private int? firstID;
}

class second
{
    private int secondID;
    private int secondField;
}

public override Expression<Func<first, bool>> FirstFilter()
{
    Contex db = new Contex();
    List<second> list = (from p in db.second select p).ToList();

    return b => list.Select(p => p.secondID).Contains(b.firstID);
}
使用以下命令:

return b => list.Select(p => p.secondID).Contains(b.firstID ?? 0);
但是,请注意,这可能不是我们想要的。如果第一个列表包含0,并且firstID为null,则结果将为true,因为当值为null时,您将传入0。当firstID为null时,表达式的强制转换版本将返回false。

使用以下方法:

return b => list.Select(p => p.secondID).Contains(b.firstID ?? 0);

但是,请注意,这可能不是我们想要的。如果第一个列表包含0,并且firstID为null,则结果将为true,因为当值为null时传递0。当firstID为null时,表达式的强制转换版本将返回false。

太完美了,非常感谢,我知道哪里有问题,但我不知道如何强制转换,这样的东西会使堆栈处于顶部!太好了,非常感谢,我知道哪里有问题,但我不知道如何解决,伙计,像这样的东西会使堆栈顶部!