这是我试图用实体框架完成的一个例子。有没有办法做到这一点?
//This throws an exception because I'm including Items1 twice. But I need both Lists for each Item1
context.MainItems.Include(x=>x.Item1s.Select(y=>y.SubItems1.Select(z=>z.SubSubItem)))
.Include(x=>x.Item1s.Select(y=>y.SubItems2))
Run Code Online (Sandbox Code Playgroud) 我有这种情况,在if和else if块中声明了相同的变量.问题是绕过if语句,执行移动到else if语句,然后不允许重新分配变量.这是一个简单的例子.
if (filename.ToLower().Contains(".tif"))
{
int test = 0;
string test1;
}
else if (ValidExtension(filename.ToLower()))
{
int test = 1;
//test still equals 0? Not possible right?
string test1 = "hello";
//test1 still equals null? Again not possible..?
//EDIT:
//Code that accesses test1 here, but test1 = null unless renamed to anything but test1...
}
Run Code Online (Sandbox Code Playgroud)
ValidExtension只是检查"filename"的扩展名是否是".tif"的任何其他图像格式.它返回true或false.如果重要,则此代码在GUI线程以外的其他线程上执行.
任何人都可以解释为什么或如何发生这种情况的可能原因?
编辑:
我完全理解这些是范围变量我很清楚微软的文档.在整个编码生涯中,我已经做了很多次,并且工作正常.我只是有一个奇怪的情况,出于某种原因,如果我在它下面的一些代码中使用字符串test1 ="hello",仍然在Else If块中,它返回为null.我对Microsoft文档的理解是test1 ="hello"下的行,test1应该总是="hello"但是它没有,它仍然等于null.但是,如果我将代码更改为test2 ="hello",那么它实际上是"你好".我的问题可能措辞不多,显然不是很清楚.这不是一个菜鸟问题,我想知道这会是什么样的错误?
编辑:
这是正在运行的实际代码..希望这将为问题提供更好的背景.
if (filename.ToLower().Contains(".tif"))
{
using (BaseBL.BeginImpersonate())
{
string output = Path.Combine(BLSettings.ClaimFilesPendingIndexingFolder, string.Format("{0}¡{1}¡{2}", "FileDrop", BaseBL.ApplicationUser, filename)); …Run Code Online (Sandbox Code Playgroud)