LINQ查询与条件WHERE子句 [英] Linq query with a Conditional WHERE Clause

查看:1383
本文介绍了LINQ查询与条件WHERE子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的LINQ等等道歉前期



我有以下的LINQ查询: -



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



我需要包括在上面的查询where子句......只有一个变量被称为
BranchKeySelected是



我已经使用If Else语句尝试和有一个包含where子句和一个不能与
1个重复上述相同的查询。 ......但是,当我做到这一点..然后OrdersByBranches
不是if语句外提供



会感谢任何帮助



问候




解决方案

从上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; 

这篇关于LINQ查询与条件WHERE子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆