Click here to Skip to main content
13,344,290 members (53,912 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 22 Jul 2008


, 22 Jul 2008
Rate this:
Please Sign up or sign in to vote.
A WCF web service that queries a database table using LINQ, returning the resulting collection


In this article, a web service with HTTP binding is created, exposing a method that queries a database table using LINQ. The method is defined in the service contract of the WCF service. Each resulting record from the LINQ query is populated in an object that is defined in a data contract of the WCF service. The collection of objects is then returned by the service method as a generic IEnumerable collection.

Defining the WCF Service

We first create the WCF service in Visual Studio 2008 and version 3.5 of .NET, using the wsHttp binding. The service is defined in the web.config file, as follows:

   <service name="SomeService" behaviorConfiguration="ServiceBehavior">
       <endpoint address=""  binding="wsHttpBinding" contract="ISomeService">

Note how the address attribute of the endpoint is left blank for now, letting IIS use a default address. This can be changed to a fixed address at deployment time.

We then define the service contract in the ISomeService.cs file. The service contract is where all the service methods are defined:

public interface ISomeService
   string GetData(int value);

   IEnumerable<EmpRecord> GetEmpData(string empLastName);

We have two methods defined, the first one is just a simple method that returns a string based on an int passed in as an argument. The second method, which is the one that is of real interest to us, accepts a last name of an employee and returns a collection of EmpRecord objects. The EmpRecord class is defined in the data contract of the service, also in the ISomeService.cs file, as follows:

public class EmpRecord
   int id = 0;
   string fName = "";
   string lName = "";
   string state = "";
  public int Id
     get { return id; }
     set { id = value; }
  public string First
     get { return fName; }
     set { fName = value; }
  public string Last
     get { return lName; }
     set { lName = value; }
  public string State
     get { return state; }
     set { state = value; }

The EmpRecord class is defined as a data contract in the WCF service, with four private fields and four corresponding public properties to get and set the field values.

In the SomeService.cs file of the WCF project, we implement the service interface and methods that we defined above, as follows:

public class SomeService : ISomeService
   public string GetData(int value)
       return string.Format("You entered: {0}", value);
   public IEnumerable<EmpRecord> GetEmpData(string empLastName)
       LinqDataClassesDataContext ctxt = new LinqDataClassesDataContext();
       return from r in ctxt.Employees
              where r.lName.Contains(empLastName)
              select new EmpRecord 
		{ Id = r.ID, First = r.fName, Last = r.lName, State = r.state};

A LINQ expression is used in the body of the GetEmpData method to query the Employee table, returning all rows that have their lName fields containing the last name string passed in as an argument. The Employee table is made up of the following fields: ID, fName, lName, City, and State. All matching rows are returned by the method as an IEnumerable collection. The LinqDataClassesDataContext is defined in the DBML file of our WCF service project, which is basically a LINQ to SQL data context object. We created this data context object by visually dropping our database tables into the LINQ to SQL DBML file that we added to our WCF project.

Consuming the WCF Service

To consume the WCF service, we create an ASP.NET web application project, and add a service reference to it in the Solution Explorer. We then create an instance of the service and call the appropriate methods, such as:

protected void Page_Load(object sender, EventArgs e)
   SomeSvc.SomeServiceClient someSrvc = new SomeSvc.SomeServiceClient();
   var coll = someSrvc.GetEmpData("Evans");

   GridView2.DataSource = coll;

SomeSvc is the name of the service reference that I added to my ASP.NET web app project, which gives us access to the client proxy class. I then call the GetEmpData method, passing an employee last name, and bind the returned collection of matching EmpRecord objects to a gridview.

Alternatively, we can just iterate through the collection of EmpRecord objects, as shown below:

bool bFound = false;
foreach (SamSvc.EmpRecord empl in coll)
    if (empl.State.IndexOf("FL") >= 0)
        bFound = true;

Points of Interest

  • The simplicity of defining our own objects as data contracts in a WCF service, and making use of easy to use LINQ expressions to encapsulate returned database records in those objects.
  • Similarly, service contracts make it fairly straightforward to define our own methods in the WCF service, accepting and returning our own defined objects.


  • 22nd July, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sam Haidar
Software Developer (Senior)
United States United States
.Net developer in the South Florida area since 2000. Prior to .Net, mainly working with C++, MFC and COM.

You may also be interested in...

Comments and Discussions

QuestionWCF with EntityRef 1:Many ? Pin
Penang17414-Apr-09 7:41
memberPenang17414-Apr-09 7:41 
GeneralNice Article Pin
rilov12-Dec-08 16:17
memberrilov12-Dec-08 16:17 
very nice article....

'I have not failed. I've just
found 10,000 ways that won't work.'
-Thomas Edison

Generalwcf webservice with .net 2.0 Pin
Member 32396614-Aug-08 3:10
memberMember 32396614-Aug-08 3:10 
GeneralRe: wcf webservice with .net 2.0 Pin
Sam Haidar11-Aug-08 2:58
memberSam Haidar11-Aug-08 2:58 
AnswerRe: wcf webservice with .net 2.0 Pin
JohnWSaundersIII27-Sep-08 7:26
memberJohnWSaundersIII27-Sep-08 7:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 22 Jul 2008
Article Copyright 2008 by Sam Haidar
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid