asp.net 如何在一个查询中使用Where条件And,OR选择sql

2jcobegt  于 2022-11-19  发布在  .NET
关注(0)|答案(1)|浏览(184)

我想在Linq中的一个查询中查询sql where Condition And和Or。我尝试在下面编写代码,但没有工作。我想在CEvaUserEva或EEvaUserEva或OEvaUserEva中查询where ID,但状态必须为“成功”请帮助我。

public ActionResult Get(string id)
{
    try
    {

        var result = (
            from pre in context.PreChanges
            join cus in context.PreEvaCustomers on pre.DocNumber equals cus.CDocNumber
            join env in context.PreEvaEnvis on pre.DocNumber equals env.EDocNumber
            join oth in context.PreEvaOthers on pre.DocNumber equals oth.ODocNumber

            join sta in context.StatusDocs on pre.DocNumber equals sta.SDocNumber
            select new GetAll
            {
                
                DocNumber = pre.DocNumber,
                Users = pre.Users,
                UserId = pre.UserId,
                CDocNumber = cus.CDocNumber,
                CEvaUserEva = cus.CEvaUserEva,
                EDocNumber = env.EDocNumber,
                EEvaUserEva = env.EEvaUserEva,
                ODocNumber = cus.CDocNumber,
                OEvaUserEva = oth.OEvaUserEva,
                SDocNumber = sta.SDocNumber,
                StatusDoc =sta.StatusDoc

            })
            .Where((g => g.CEvaUserEva == id || g.EEvaUserEva == id ||
                g.OEvaUserEva == id  && (g => g.StatusDoc== "Success"));

        return Ok(result);
    }
    catch (Exception ex)
    {
        return BadRequest(ex.Message + "GetC 47");
    }
}
2w2cym1i

2w2cym1i1#

您需要在或区块前后加上括号

.Where(g => (g.CEvaUserEva == id || g.EEvaUserEva == id ||
            g.OEvaUserEva == id) && g.StatusDoc == "Success");

相关问题