Posts
76
Comments
310
Trackbacks
7
List.Find and Predicates

Over the holiday I began to dive into WCF (Windows Communication Foundation).  A few months ago, I created a simple Zip Code Lookup service using ThinkTecture's WSCF (Web Service Contract First add-in).  The Zip Code Lookup service is the perfect project to upgrade to WCF. 

One of the operations I added to the Zip Code Lookup service is called 'GetStateCodes'.  The GetStateCodes operation retrieves a list of State Codes (ex: 'FL', 'Jacksonville') see code below.  BTW, I'm also using SubSonic for the DAL.

Ok, I need a unit test as well and this is where I used List.Find to return an instance of StateCode from List<StateCode>.  Normally, I'd write a ForEach statement in a separate method to find an object in a collection.  However, now with Generics things are much easier.

Here is another reference from Developer Fusion that demonstrates sorting and searching a generic list.

1
2
3
4
5
6
7
8
9
10
11
12
13
      [TestMethod()]
public void SearchByState()
{
ZipCodeService svc = new ZipCodeService();
ZipCodeSearchRequest request = new ZipCodeSearchRequest();

List<StateCode> states = svc.GetStateCodes();
request.State = states.Find(delegate(StateCode state) { return state.StateAbbr == "FL"; });

List<ZipCodeInfo> actual = svc.ZipCodeSearch(request);

Assert.IsTrue(actual.Count > 0);
}

Operation Implementation Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public List<StateCode> GetStateCodes()
{
// Execute search and load results
Query qry = new Query(ZipCode.Schema);
qry.SelectList = ZipCode.Columns.StateCode + ", " + ZipCode.Columns.StateName;
qry.OrderBy = OrderBy.Asc(ZipCode.Columns.StateCode);

List<StateCode> stateInfo = null;
using (IDataReader dr = qry.ExecuteReader())
{
// Translate collection to WCF datacontract
stateInfo = translate.StateInfo(dr);
}

// Return results to happy consumer
return stateInfo;
}

DataContract for StateCode:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   [DataContract(Namespace = Namespaces.ZipCodeNamespace)]
public class StateCode
{
private string stateAbbrField;
private string stateNameField;

public StateCode() {}

public StateCode(string stateAbbr, string stateName)
{
StateAbbr = stateAbbr;
StateName = stateName;
}

[DataMember(Order = 0)]
public String StateAbbr { get { return stateAbbrField; } set { stateAbbrField = value; } }

[DataMember(Order = 1)]
public string StateName { get { return stateNameField; } set { stateNameField = value; } }
}
posted on Monday, January 01, 2007 11:09 PM Print
Comments
Gravatar
# re: List.Find and Predicates
HoweMarjorie33
12/23/2010 7:07 PM
  
You need to waste a lot of time to complete your research papers, however a distinguished term paper essays service would do it much faster. Why perform term papers? Wtiting firms will do it for you.
Gravatar
# re: List.Find and Predicates
forex en ligne
12/9/2011 7:04 AM
  
Normally, I'd write a ForEach statement in a separate method to find an object in a collection. However, now with Generics things are much easier.
Gravatar
# re: List.Find and Predicates
culinary training
3/26/2012 6:22 AM
  
Yeah, that is one of the basic steps I've ever read. Thank you so much for sharing your thoughts. :)
Gravatar
# re: List.Find and Predicates
hvac training
5/15/2012 12:28 PM
  
Those are indeed very useful code. I hope I could see more of your work! Nicely done!

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 1 and 5 and type the answer here: