C# 无法从“System.Collections.Generic.IEnumerable”转换为“System.Collections.Generic.IEnumerable”
我有一个错误: 无法从“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
我尝试了许多不同的方法,但我不知道如何修复它。如果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。太完美了,非常感谢,我知道哪里有问题,但我不知道如何强制转换,这样的东西会使堆栈处于顶部!太好了,非常感谢,我知道哪里有问题,但我不知道如何解决,伙计,像这样的东西会使堆栈顶部!