Using LINQ “Contains” to Query WCF Data Services

Working on a client project recently I ran into a problem trying to query my WCF Data Service.  I get a list of counties from an ASP .NET MVC 2 application and need to find the matching members who live within those counties.  The obvious solution would be something like this:

var countyList = new [] { "SomeCounty", "AnotherCounty", "LastCounty" };
var query = from member in Context.Members
            where countyList.Contains(member.County)
            select member;

But wait! It seems WCF doesn’t support using the Contain method like this.  My solution was to convert the array of counties into a single delimited string and call Contains against that.

var counties = "";
foreach (var county in countyList) {
    counties += county + ";";
}
var query = Context.Members.Where(m => counties.Contains(m.County));

This works, but in my opinion it’s much less intuitive of a solution, probably doesn’t scale well, and will certainly be more difficult to maintain.  So my love / hate relationship with LINQ continues (although to be fair, this is probably more of a limitation of WCF Data Services…).  I hope this helps someone who might find themselves in the same spot I did.

× Comments are closed.