Database table structure
What do I want to achieve?
- 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.
- 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.
- In row one in Column3 has the “No” status should give data4a and the No status in column7 should give data8a from column8.
- 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
}
