As we have a working Data Access Object, we can now go in and create the custom methods we need for a particular function.
Open up the
ContactDataControl.cs
orContactDataControl.vb
again, and let's go down to the bottom of the class, just before the end.Let's create a new function to return all of the Contacts that have addresses in a particular state, and we will pass that state with a
string
variable. Add the following method to your code:public IList<Contact> GetByState(string State) { ICriteria criteria = Session.CreateCriteria<Contact>(); criteria.CreateCriteria(FieldNames.Addresses).Add(Restrictions.Eq(AddressDataControl.FieldNames.State, State)); return criteria.List<Contact>(); }
If you are working in VB.NET, add the following code:
Public Function GetByState(ByVal State As String) As IList(Of Contact) Dim criteria As ICriteria = Session.CreateCriteria(Of Contact)() criteria.CreateCriteria(FieldNames.Addresses...