如何从 wcf ria 服务中的表中选择字段 [英] How to select fields from table in wcf ria services

查看:27
本文介绍了如何从 wcf ria 服务中的表中选择字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用使用 wcf ria 服务的 Silverlight 业务应用程序.

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;
    }

此方法返回表中的所有字段,我可以将其绑定到数据网格.表包含员工 ID、员工姓名和年龄字段.

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.

我该怎么做?

推荐答案

(抱歉我的英语不好)

如果您的方法返回类型是 IQueryable你必须返回 IQueryable.如果你想返回其他类型,你可以这样做:

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; }
 }

然后创建查询方法:

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

现在您可以在客户端上加载查询,它将返回一个 EmployeeDTO 列表(只有姓名和年龄)

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

这篇关于如何从 wcf ria 服务中的表中选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆