Search code examples
silverlight-4.0wcf-ria-services

How to select fields from table in wcf ria services


I am using a silverlight business application using wcf ria services.

in my domain class there is a method

 public IQueryable<Employee> GetEmployees()
    {
        return this.ObjectContext.Employees;
    }

this method returns al the field in the table and i can bind it to datagrid. table contains employee id,employee name and age fields.

Now i want to take only one or two fields from this table.

i mean i need employee name and age ,not id. or i need to use employee name to bind to combobox.

How can i do this?


Solution

  • (sorry for my bad english)

    If you method return type is IQueryable<Employee> you have to return IQueryable<Employee>. If you want to return other type you can do:

     public class EmployeeDTO
     {
        [Key]
        public int Id { get; set; } //<-- you need a key for this to work
        public string Name { get; set; }
        public int Age { get; set; }
     }
    

    And then create a query method:

    public IQueryable<EmployeeDTO> GetEmployeeDTO()
    {
        return this.ObjectContext.Employees.Select(e=> new EmployeeDTO { Name = e.Name, Age = e.Age});
    }
    

    Now you can load the query on the client and it will return a list of EmployeeDTO (with only Name and Age)