且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LINQ查询与条件WHERE子句

更新时间:2022-10-15 08:58:08

从上a.BranchKey AllBranchKeys一个在AllOrders 
JOIN b VAR OrdersByBranches =
等于b.BranchKey
排序依据a.Date
通过新的{a.Date,a.paymentMethod}成BranchOrderGrouped
组中选择新的{
BranchOrderGrouped.Key.Date,
CurrencyAmount = BranchOrderGrouped.Sum(A => a.CurrencyAmount) ,
BranchOrderGrouped.Key.paymentMethod
};

如果(string.IsNullOrEmpty(BranchKeySelected))
{
OrdersByBranches = OrdersByBranches.Where(/ * * blbabla /);
}

返回OrdersByBranches;


I am new to LINQ so apologises upfront

I have the following Linq query :-

var OrdersByBranches =
    from a in AllOrders
    join b in AllBranchKeys
        on a.BranchKey equals b.BranchKey
    orderby a.Date
    group a by new { a.Date, a.paymentMethod } into BranchOrderGrouped
    select new
    {
        BranchOrderGrouped.Key.Date,
        CurrencyAmount = BranchOrderGrouped.Sum(a =>
        a.CurrencyAmount),
        BranchOrderGrouped.Key.paymentMethod
    };

I need to include a where clause in the above query ... only if a variable called BranchKeySelected is ""

I have tried using an If Else statement and have the same above query duplicated with one containing a where clause and one NOT. ...But When I do this .. then OrdersByBranches is NOT available outside of the IF Statement

Would be grateful for any help

Regards

Ghost

var OrdersByBranches = 
    from a in AllOrders 
    join b in AllBranchKeys on a.BranchKey equals b.BranchKey 
    orderby a.Date 
    group a by new { a.Date, a.paymentMethod } into BranchOrderGrouped 
    select new { 
        BranchOrderGrouped.Key.Date, 
        CurrencyAmount = BranchOrderGrouped.Sum(a =>  a.CurrencyAmount), 
        BranchOrderGrouped.Key.paymentMethod 
    };

if(string.IsNullOrEmpty(BranchKeySelected ))
{
    OrdersByBranches = OrdersByBranches.Where(/*blbabla*/);
}

return OrdersByBranches;