Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.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# 如何访问查询变量_C#_.net_Visual Studio_Linq - Fatal编程技术网

C# 如何访问查询变量

C# 如何访问查询变量,c#,.net,visual-studio,linq,C#,.net,Visual Studio,Linq,如何读取if范围之外的查询变量并进一步使用它 If (IsPaymentList) { var query = paymentList.GroupBy( grp => (grp.GrouByField), (GrouByField, paymentListgroup) => new { Key = GrouByField, r

如何读取if范围之外的查询变量并进一步使用它

If (IsPaymentList)
{
            var query = paymentList.GroupBy(
            grp => (grp.GrouByField),
            (GrouByField, paymentListgroup) => new
            {
                Key = GrouByField,
                receiptAmount = paymentListgroup.Sum(fields => fields.PaymentAmount),
                creditAccountnumber = paymentListgroup.Max(fields => fields.CreditAccountNumber),
                bankAccountName = paymentListgroup.Max(fields => fields.AccountName),
                bankName = paymentListgroup.Max(fields => fields.BankName),
                payCurrency = paymentListgroup.Max(fields => fields.PaymentCurrencyID),
                partnerServiceID = paymentListgroup.Max(fields => fields.PartnerServiceID),

            });
}
有人请分享你的经验。
谢谢

您需要做的是将查询变量移出if语句的上下文

这是一个关于如何使用整数列表执行此操作的示例:

var list = new [] { 10, 3, 2, 4, 6, 7, 8 };

var condition = false;

IEnumerable<IGrouping<int,int>> query;

if(condition)
{
    query = list.GroupBy(x => x);
}
由于
y
位于
x
以外的另一个作用域中,因此无法编译,您可以通过将
y
的偏差移动到该作用域之外来解决此问题。像这样:

var x = 10;
int y;
{
    y = 20;
}

Console.WriteLine(x+y);

在这种情况下,在声明
y
时不能使用
var
,因为这意味着您也要告诉编译器它是什么类型的。而你不是。所以写
vary没有任何意义,但是
vary=10需要做的是将查询变量移出if语句的上下文

这是一个关于如何使用整数列表执行此操作的示例:

var list = new [] { 10, 3, 2, 4, 6, 7, 8 };

var condition = false;

IEnumerable<IGrouping<int,int>> query;

if(condition)
{
    query = list.GroupBy(x => x);
}
由于
y
位于
x
以外的另一个作用域中,因此无法编译,您可以通过将
y
的偏差移动到该作用域之外来解决此问题。像这样:

var x = 10;
int y;
{
    y = 20;
}

Console.WriteLine(x+y);

在这种情况下,在声明
y
时不能使用
var
,因为这意味着您也要告诉编译器它是什么类型的。而你不是。所以写
vary没有任何意义,但是
vary=10您可以将查询变量声明为类型
IQueryable查询=null
在if外部,并将其设置为null,其中typeOfKey是grp.groupyField属性的类型。您不必选择匿名类型,而必须创建一个具有所需属性的新类,如下所示:

public class NewClass
{
     public int Key {get;set;}
     public decimal ReceiptAmount {get;set;}
     //add all the properties here

     public NewClass(string key,decimal recieptAmount)
     {
          //and add constructor for all the needed properties
     }
}
创建类后,将查询设置为泛型类型

//i used int for your grp.GrouByField type
IQueryable<IGrouping<int,NewClass>> query = null;
if(IsPaymentList)
{
    query = paymentList.GroupBy(
            grp => (grp.GrouByField),
            (GrouByField, paymentListgroup) => 
            new NewClass(GrouByField, 
            paymentListgroup.Sum(fields => fields.PaymentAmount), 
            paymentListgroup.Max(fields => fields.CreditAccountNumber), 
            paymentListgroup.Max(fields => fields.AccountName), 
            paymentListgroup.Max(fields => fields.BankName), 
            paymentListgroup.Max(fields => fields.PaymentCurrencyID), 
            paymentListgroup.Max(fields => fields.PartnerServiceID)));
}
//我对grp.GrouByField类型使用了int
IQueryable查询=null;
如果(iPaymentList)
{
query=paymentList.GroupBy(
grp=>(grp.GrouByField),
(GroupyField,paymentListgroup)=>
新类(GrouByField,
paymentListgroup.Sum(字段=>fields.PaymentAmount),
paymentListgroup.Max(字段=>fields.CreditAccountNumber),
paymentListgroup.Max(fields=>fields.AccountName),
paymentListgroup.Max(fields=>fields.BankName),
paymentListgroup.Max(fields=>fields.PaymentCurrencyID),
paymentListgroup.Max(fields=>fields.PartnerServiceID));
}

您可以将查询变量声明为类型
IQueryable查询=null
在if外部,并将其设置为null,其中typeOfKey是grp.groupyField属性的类型。您不必选择匿名类型,而必须创建一个具有所需属性的新类,如下所示:

public class NewClass
{
     public int Key {get;set;}
     public decimal ReceiptAmount {get;set;}
     //add all the properties here

     public NewClass(string key,decimal recieptAmount)
     {
          //and add constructor for all the needed properties
     }
}
创建类后,将查询设置为泛型类型

//i used int for your grp.GrouByField type
IQueryable<IGrouping<int,NewClass>> query = null;
if(IsPaymentList)
{
    query = paymentList.GroupBy(
            grp => (grp.GrouByField),
            (GrouByField, paymentListgroup) => 
            new NewClass(GrouByField, 
            paymentListgroup.Sum(fields => fields.PaymentAmount), 
            paymentListgroup.Max(fields => fields.CreditAccountNumber), 
            paymentListgroup.Max(fields => fields.AccountName), 
            paymentListgroup.Max(fields => fields.BankName), 
            paymentListgroup.Max(fields => fields.PaymentCurrencyID), 
            paymentListgroup.Max(fields => fields.PartnerServiceID)));
}
//我对grp.GrouByField类型使用了int
IQueryable查询=null;
如果(iPaymentList)
{
query=paymentList.GroupBy(
grp=>(grp.GrouByField),
(GroupyField,paymentListgroup)=>
新类(GrouByField,
paymentListgroup.Sum(字段=>fields.PaymentAmount),
paymentListgroup.Max(字段=>fields.CreditAccountNumber),
paymentListgroup.Max(fields=>fields.AccountName),
paymentListgroup.Max(fields=>fields.BankName),
paymentListgroup.Max(fields=>fields.PaymentCurrencyID),
paymentListgroup.Max(fields=>fields.PartnerServiceID));
}

如果使用VAR,则不可能。我能想到的最干净的解决方案是使用命名类型。这与查询无关,但与基本编程知识有关。请查找作用域:@PoweRoy我相信OP会知道如何将普通变量移到作用域之外。这里的问题是变量隐式类型化为匿名类型。因此,在保持初始化的同时,不能简单地移动声明。如果使用VAR,则不可能。我能想到的最干净的解决方案是使用命名类型。这与查询无关,而与基本编程知识有关。请查找作用域:@PoweRoy我相信OP会知道如何将普通变量移到作用域之外。这里的问题是变量隐式类型化为匿名类型。因此,您不能在保持初始化的同时简单地移动声明。现在您丢失了
query
@CodeInChaos的静态类型,这样更好?现在您丢失了
query
@CodeInChaos的静态类型,这样更好?