0

Database table structure

Database table structure

What do I want to achieve?

  1. I am using the Linq query from the view component. I have to check if there is the “No” status in Column1 then data2b from Column2 should be selected. Likewise, if there is the “No” status in Column3 then data4a and data4c from Column4 should be selected. The same would go with Column6 and Column8.
  2. So Column1 and Colum2, Column3 and Colum4, Column5 and Colum6 and Column7 and Colum8 are conditionally dependent to each other. Column1’s status should not affect column4.
  3. In row one in Column3 has the “No” status should give data4a and the No status in column7 should give data8a from column8.
  4. The status is stored as the enum.

So far this is what I have tried:

public class NotOkeyViewComponent : ViewComponent
{
    private readonly DbContext _context;
    public NotOkeyViewComponent(DbContext context)
    {
        _context = context;
    }
    public Tuple<int, Guid> getFormInfo()
    {
        var CustomerGuid = Guid.Parse((string)TempData["CustomerId"]);
        var FormId = _context.Forms
        .Where(t => t.CompanyId == CustomerGuid)
        .Where(t => t.FormType.Contains("CustomerForm"))
        .Select(t => t.Id)
        .First();
        TempData.Keep();
        return new Tuple<int, Guid>(FormId, CustomerGuid);
    }
    public IViewComponentResult Invoke()
    {
        var FormTuple = getFormInfo();
        var query = _context.Dbtables
                .Where(x => x.RecStatus == 'A' &&
                        x.Form.Id == FormTuple.Item1 &&
                        x.Form.CompanyId == FormTuple.Item2) // Item2: CustomerGuid; 
                .Where(x => x.Column1.Equals(StatusEnum.Ei))                        
                .Select(x => x.Column2)  
                .Where(x => x.Column3.Equals(StatusEnum.Ei))
                .Select(x => x.Column4);                        
        TempData.Keep();
        List<Dbtable> model = query.ToList();
        return View(model);
    }
}

When I use the Where query, this returns false and none of the columns will be selected, which I don't want. I still want it checks Column3, Column5 and Column7 and selects from Column4, Column6 and Column8 if the condition satisfys. How can I make it so that Column1 is only dependent to Column2 and all the queries can be done at once?

// StatusEnum
public enum StatusEnum 
{ 
    [Display(Name ="Yes")]
    Yes = 0,
    [Display(Name = "No")]
    No = 1,
    [Display(Name = "Not Known")]
    Not_Known = 2
}
1
  • Just FYI before I read more of the question: Stack Overflow now supports table markdown, so no need for a screenshot of the table Commented Mar 29, 2021 at 10:19

1 Answer 1

0

This query can be easily done via conditional operator. You do not need Where which filters records, but you need to choose appropriate value from the record.

_context.Dbtables
    .Where(x => x.RecStatus == 'A' &&
            x.Form.Id == FormTuple.Item1 &&
            x.Form.CompanyId == FormTuple.Item2)
    .Select(x => 
        x.Column1 == StatusEnum.Yes ? x.Column2 : 
        x.Column3 == StatusEnum.Yes ? x.Column4 :
        x.Column5 == StatusEnum.Yes ? x.Column6 :
        x.Column7 == StatusEnum.Yes ? x.Column8 :
        null
    );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your suggestion. This query returns strings, but I am in need of List<dbtable> how can I get it in List format?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.