简体   繁体   English

如何声明要在C#表达式上使用的全局变量

[英]How to declare a global variable to be used on a C# expression

I´m doing this : 我正在这样做:

protected void btn_Click(object sender, EventArgs e){

var myQuery = from tab1 in Table
              where myConditions
              select tab1 ;

After that, I´m filtering with user selected choices : 之后,我将过滤用户选择的选项:

myQuery.Where(x => x.someField == someChoice);

It´s working fine. 工作正常。 But I would like to declare the "myQuery" with global escope. 但是我想用全局范围声明“ myQuery”。

How should I do that ? 我该怎么办? I´m trying to put the line below inside the class and outside the method, but I don´t know what type should I use : 我正在尝试将行放在类内部和方法之外,但是我不知道应该使用哪种类型:

private someTypeThatIdontKnow myQuery;

TIA. TIA。

The sneaky way to do this is to make it: 偷偷摸摸的方法是:

this.myQuery = ...

then position the caret at the myQuery (which will have a red squiggly, indicating a problem), and press ctrl + . 然后将插入符号放置在myQuery (它将呈红色波浪状表示问题),然后按ctrl + , and select the IDE's offer to generate a field: ,然后选择IDE的报价以生成一个字段:

在此处输入图片说明

This generates a field of the correct type: 这将生成正确类型的字段:

在此处输入图片说明

Now just move the query itself into where-ever you need it (presumably a constructor), after MyTable has been set. 现在,在设置MyTable之后,只需将查询本身移到所需的任何位置(可能是构造函数)即可。

If you hover your mouse over the var keyword it will tell you what type it is. 如果将鼠标悬停在var关键字上,它将告诉您它是什么类型。

You can then use that to define a member for the class to hold the result. 然后,您可以使用它来定义类的成员以保存结果。

You can use the var keyword and obtain the type and then declare the variable of that type. 您可以使用var关键字并获取类型,然后声明该类型的变量。

Or, if from some reason you do not want to do it that way, you could use a dynamic type, but I would advise against that. 或者,如果出于某种原因您不想这样做,则可以使用dynamic类型,但我建议您不要这样做。

The best solution would be to let the compiler infer the type with the var keyword and then use that type explicitly as the class member (the "global variable" as you referred to it). 最好的解决方案是让编译器使用var关键字推断类型,然后将该类型显式用作类成员(即您所指的“全局变量”)。

LINQ查询返回某种IEnumerable,所以我相信它将

private IEnumerable<TypeOfObjectInYourCollection> myQuery;

Use IQueryable<TableEntity> , where TableEntity is the type of the objects in your Table DbSet . 使用IQueryable<TableEntity> ,其中TableEntityTable DbSet对象的类型。

It may also be an IEnumerable<TableEntity> , depending on whether you run your query against a database or some in-memory collection. 它也可以是IEnumerable<TableEntity> ,这取决于您是针对数据库还是针对内存中集合运行查询。

You can also see which type to use by hovering your mouse over myQuery . 您也可以通过将鼠标悬停在myQuery查看要使用的类型。

The type will be an IQueryable of type tab1 . 该类型将是类型为tab1IQueryable

Assuming you eventually call ToList() to run the query, you'll end up with a List<tab1> . 假设您最终调用ToList()来运行查询,最终将得到List<tab1> So you can declare your global variable as that type. 因此,您可以将全局变量声明为该类型。

I'm assuming tab1 is an object that's available for you to have a list of. 我假设tab1是一个可供您使用的对象列表。

List<tab1> result = new List<tab1>();

protected void btn_Click(object sender, EventArgs e)
{
    var myQuery = from tab1 in Table
                  where myConditions
                  select tab1;

    myQuery.Where(x => x.someField == someChoice);

    result = myQuery.ToList();
}

试试EnumerableRowCollection

 private EnumerableRowCollection query

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

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