开发者

How to access the query variable

开发者 https://www.devze.com 2023-03-18 00:21 出处:网络
how can I read the query variable outside the if scope and use it further. If (开发者_StackOverflowIsPaymentList)

how can I read the query variable outside the if scope and use it further.

If (开发者_StackOverflowIsPaymentList)
{
            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),

            });
}

Somebody please share your experience. Thanks


What you need to do is to move the query-variable out of the context of the if-statement.

This is an example on how you do that with an integer list:

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);
}

Then I can continue query the queryvariable like this after the if-statement if I so like:

query.Take(2)

The problem is here that you want to use the query variable outside of the scope that you are creating it.

Consider the following:

static void Main(string[] args)
{
    var x = 10;

    {
        var y = 20;
    }

    Console.WriteLine(x+y);
}

This wont compile because y is in another scope than x, you solve this by moving the decleration of y outside of that scope. Like this:

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

Console.WriteLine(x+y);

You can't use var when declaring y in this case because that would imply that you are also telling the compiler what kind of type it is. And you are not. So writing var y; wouldn't make any sense, but var y = 10; does because now the compiler knows that this is in fact an integer.


You can declare the query variable as of type IQueryable<IGrouping<typeOfKey,typeOfSelect>> query = null; outside the if and set it to null where the typeOfKey is the type of the grp.GrouByField property. Instead of selecting an anonymous type you have to create a new class that has the properties as needed like this:

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
     }
}

After you create the class set the query to the generic type

//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)));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消