Linq - Boolean - where语句

时间:2017-06-16 09:57:48

标签: c# linq boolean

我有以下代码,我正在努力。

var q = from hed in cxt.SOPOrderReturns.ToExpandable()
        join cus in cxt.SLCustomerAccounts 
              on hed.CustomerID equals cus.SLCustomerAccountID
        join ad in cxt.SOPDocDelAddresses 
              on hed.SOPOrderReturnID equals ad.SOPOrderReturnID
        where hed.AnalysisCode1 == "SO"
        select new
        {
            hed.SOPOrderReturnID,
            hed.DocumentNo,
            hed.DocumentDate,
            cus.CustomerAccountNumber,
            Route = hed.AnalysisCode2,
            Drop = hed.AnalysisCode5,
            hed.ReadyForInvoicePrint,
        };

q = q.RemoveExpandable();

return q;

列hed.ReadyForInvoicePrint是布尔值,我想添加一个where语句,该语句还显示列是否为FALSE。

由于

杰米

2 个答案:

答案 0 :(得分:1)

您可以在q语句中添加false来过滤此列的&& !hed.ReadyForInvoicePrint值。

答案 1 :(得分:0)

根据您的要求,我相信您希望能够查询许多参数。您只需添加一个变量即可。

var q = from hed in cxt.SOPOrderReturns.ToExpandable()
var someVar = true | false;
join cus in cxt.SLCustomerAccounts on hed.CustomerID equals 
cus.SLCustomerAccountID
join ad in cxt.SOPDocDelAddresses on hed.SOPOrderReturnID equals 
ad.SOPOrderReturnID
where hed.AnalysisCode1 == "SO" && hed.ReadyForInvoicePrint == someVar

select new
{
hed.SOPOrderReturnID,
hed.DocumentNo,
hed.DocumentDate,
cus.CustomerAccountNumber,
Route = hed.AnalysisCode2,
Drop = hed.AnalysisCode5,
hed.ReadyForInvoicePrint,

};

q = q.RemoveExpandable();

return q;

或者,您可以将查询包装到IQueryable中,其中子句可以在代码中稍后有条件地添加。

private IQueriable<SOPOrderReturns> GetSOPOrderReturnsQuery(){
var q = from hed in cxt.SOPOrderReturns.ToExpandable()
join cus in cxt.SLCustomerAccounts on hed.CustomerID equals 
cus.SLCustomerAccountID
join ad in cxt.SOPDocDelAddresses on hed.SOPOrderReturnID equals 
ad.SOPOrderReturnID

select new
{
hed.SOPOrderReturnID,
hed.DocumentNo,
hed.DocumentDate,
cus.CustomerAccountNumber,
Route = hed.AnalysisCode2,
Drop = hed.AnalysisCode5,
hed.ReadyForInvoicePrint,

};

q = q.RemoveExpandable();

return q.AsQueryable();
}

获取查询并进行更改

var query = GetSOPOrderReturnsQuery();
query = query.Where(x=>x.AnalysisCode1 == "SO");
query = query.Where(x=>x.ReadyForInvoicePrint == false);

希望这有帮助。