Search code examples
c#asp.netlinq

how to select sql by use where condition And , OR in one Query


I want query sql where Condition And and Or in one Query in Linq. I try to coding in this below but not work. I want to query where id in CEvaUserEva or EEvaUserEva or OEvaUserEva but status must be 'success' Plese help me.

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");
    }
}

Solution

  • You need parenthesis around the or block

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