views:

43

answers:

2

I have a subcontract table with a company field. On the company page, I do not want the company to be able to be deleted if it is attached to an active subcontract. I am currently using the following expression to display the delete button. (Doesn't actually delete, just sets company to inactive.)

<% if (item.company1.subcontracts.Count == 0) { %>

This works for excluding all companies which are attached to subcontracts. However, my subcontract table also has an active_status field. What I really want is to be able to delete companies which are either not attached to a subcontract or are attached to an inactive subcontract (active_status == 0).

A: 

Maybe I am misunderstanding you, but it seems adding just an OR to the IF should do the trick:

<% if (item.company1.subcontracts.Count == 0 || item.company1.active_status == 0) { %>
Bradley Mountford
I don't want the active_status of the company. I want the active_status of the subcontract that it may be linked to.
RememberME
+2  A: 

How about the following:

<% var subcontracts = item.company1.subcontracts;
if (subcontracts.Count == 0 || subcontracts.Any(x => x.active_status == 0)) { %>

This solves your problem if the active_status is accessible through subcontracts

Mahesh Velaga
Yup...that's it. Alternatively you can just do item.company1.subcontracts.Any(x => x.active_status == 0) and save a line of code.
Bradley Mountford
It actually needed to be 'subcontracts.All(x => a.active_status ==0' in the case there was one active subcontract and also one inactive subcontract, I don't want to allow deletion. But, that lead me in the right direction. Thanks.
RememberME
From your question it was not clear that u wanted to delete only when all of them are inactive. Anyway, glad to help :)
Mahesh Velaga
I believe this will work as well `if (subcontracts.Count(x => x.active_status == true) == 0)`
RememberME