简体   繁体   English

LINQ2SQL:如何声明var类型的成员变量?

[英]LINQ2SQL: How do I declare a member variable of type var?

I have a class like this 我有这样的课

    public class foo
    {

        private void getThread()
        {
         var AllThreads = from sc in db.ScreenCycles
                          join s in db.Screens on sc.ScreenID equals s.ScreenID
                          select s;
        }
    }

I want to make the AllThreads variable a class variable instead of a method variable. 我想使AllThreads变量成为类变量,而不是方法变量。 Like this... 像这样...

public class foo
{
    var AllThreads;
    private void getThread()
    {
       AllThreads = from sc in db.ScreenCycles
                          join s in db.Screens on sc.ScreenID equals s.ScreenID
                          select s;
    }
}

How ever it wont let me declare a class variable of type var. 但是,它不会让我声明var类型的类变量。

How to I achieve this? 我该如何实现?

You could do it like this: 您可以这样做:

public class foo {    
  IEnumerable<string> AllThreads;    

  private void getThread() {
     AllThreads = (from sc in db.ScreenCycles
                      join s in db.Screens on sc.ScreenID equals s.ScreenID
                      select s.Screen1 + " " + sc.Thread);
  }
}

Updated per Joel's suggestion. 根据Joel的建议进行了更新。

var can only be used as a local declaration. var只能用作本地声明。 If you want to use the type returned from a LINQ expression you must have build an object. 如果要使用从LINQ表达式返回的类型,则必须已构建一个对象。

To preserve your original code, try this 要保留原始代码,请尝试此操作

public class foo

    IEnumerable<ScreenCycles> AllThreads;
    private void getThread()
    {
       AllThreads = from sc in db.ScreenCycles
                          join s in db.Screens on sc.ScreenID equals s.ScreenID
                          select s;
    }

}

The initial var is defining an anonymous class. 最初的var定义了一个匿名类。 In order to do what you what you have to define the class. 为了做什么,您必须定义类。

But...you could probably just do this: 但是...您可能只是这样做:

List<string> AllThreads;

You can't use var at the class level. 您不能在类级别使用var You need to provide an explicit type if it's not going to be initialized right away. 如果不立即初始化,则需要提供一个显式类型。

Assuming "s" is of a type name "Screen": 假设“ s”的类型名称为“ Screen”:

public class foo
{
    IEnumerable<Screen> AllThreads;
    private void getThread()
    {
       AllThreads = from sc in db.ScreenCycles
                          join s in db.Screens on sc.ScreenID equals s.ScreenID
                          select s;
    }
}

As many of said, you can't use var. 正如许多人所说,您不能使用var。 Refactor your code to use Linq types. 重构您的代码以使用Linq类型。

public class foo 
{
    ScreenCycle[] allThreads;
    private void getThread() 
    { 
        allThreads = (from sc in db.ScreenCycles join s in db.Screens on sc.ScreenID 
                     equals s.ScreenID select s).ToArray(); 
    } 
}

OK, this isn't an answer, just a relative newbie to stackoverflow question, which this thread illustrates quite well. 好的,这不是一个答案,只是stackoverflow问题的一个相对较新的问题,该线程对此进行了很好的说明。

The code markup tools on this site are seriously... um, unique. 这个网站上的代码标记工具非常认真...嗯,独一无二。 What I dont understand in this particular case is, why are all our comment codeblocks very very long? 在这种特殊情况下,我不明白的是,为什么我们所有的注释代码块都非常长? Is it something the OP did? OP做了什么? I tired various edits on my earlier example, and for some reason the textbox my code was in was much longer than its contents. 我对先前的示例进行了各种修改,由于某种原因,我的代码所在的文本框比其内容长得多。 ( IE 8 ) (IE 8)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM