gpt4 book ai didi

c# - 具有条件WHERE子句的Linq查询

转载 作者:太空宇宙 更新时间:2023-11-03 18:18:17 24 4
gpt4 key购买 nike

我是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
};


我需要在上面的查询中包括一个where子句...仅当一个名为
BranchKeySelected为“”

我尝试使用If Else语句,并使用与上述相同的重复查询
一个包含where子句和一个NOT的对象。 ...但是当我这样做..然后OrdersByBranches
在IF语句之外不可用

感谢您的帮助

问候

最佳答案

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;

关于c# - 具有条件WHERE子句的Linq查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2850024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com