c# - 从 WebAPI IQueryable 中排除列

标签 c# entity-framework asp.net-web-api

是否可以从我的 WebAPI 的 IQueryable 函数中排除一列?例如我如何从我的人员实体中排除属性“FirstName”:

[HttpGet]
public IQueryable<Contact> GetPeople()
{
    return _contextProvider.Context.People;
}

伪代码:

[HttpGet]
public IQueryable<Contact> GetPeople()
{
    return _contextProvider.Context.People.ExcludeColumn("FirstName");
}

最佳答案

手动将结果投影到 Contact 实体,并且不为 FirstName 列提供数据:

[HttpGet]
public IEnumerable<Contact> GetPeople()
{
    return from p in _contextProvider.Context.People
           select new Contact {
               Id = p.Id,
               LastName = p.LastName
           };
}

顺便说一句,我会创建一些其他没有 FirstName 属性的特定 DTO 对象。

关于c# - 从 WebAPI IQueryable 中排除列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16092951/

相关文章:

c# - 是否可以在运行时设置 C# init-only 属性?

c# - MVC @Html.Partial - 两种模型

c# - 如何反序列化此 JSON?

c# - 如何在 Controller 类中有多个 Put 动词

c# - ASP.NET 网络 API : How to authorize for static HTML content

c# - 当我知道确切的路径时,如何使用 c# 查找 XML 元素?

c# - Unity不断获取: “Can not play a disabled audio source”

c# - 当我在 ASP MVC 、 SQL 和 Entity Framework 的基础中记录时,值传递 NULL

c# - 数据绑定(bind) linq 查询到 Entity Framework 5.0 中的 datagridView

entity-framework - Entity Framework :DbContext并设置ProviderName