How to select fields from table in wcf ria services

自古美人都是妖i 提交于 2019-12-13 04:03:20

问题


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?


回答1:


(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)



来源:https://stackoverflow.com/questions/7864825/how-to-select-fields-from-table-in-wcf-ria-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!