I have two tables.
Author Table --- AuthID PK
Contract Table --- AuthID FK
I would like to restrict the Contract View "Author Name" Box to Authors that do not already have Contracts.
I think I could do it with alot more code but I feel like there is a way to do it with a simple query and I am just missing it.
Here is my Code:
public ActionResult Create()
{
var contract = new Contract();
var allAuthors = db.Authors.Select(a => a.AuthID);
var unusedAuthors = new List<Author>();
foreach (var auth in allAuthors) {
unusedAuthors = db.Contracts
.Where(a => a.AuthID.GetHashCode() != auth.GetHashCode())
.Select(a => a.Author).ToList();
}
ViewBag.AuthID = new SelectList(unusedAuthors, "AuthID", "AuthFirstName");
return View(contract);
}