带有条件WHERE子句的Linq查询

1 c# linq

我是LINQ的新手,所以要事先道歉

我有以下Linq查询: -

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
    };
Run Code Online (Sandbox Code Playgroud)

我需要在上面的查询中包含一个where子句...只有当一个名为BranchKeySelected的变量是""时

我尝试过使用If Else语句并将上述相同的查询与包含where子句和一个NOT的查询重复....但是当我这样做时...... OrdersByBranches在IF声明之外是不可用的

将不胜感激任何帮助

问候

Gre*_*ire 7

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; 
Run Code Online (Sandbox Code Playgroud)