【发布时间】:2015-07-03 01:13:07
【问题描述】:
对于给定的实体集,例如 WHO_TYPES,我想将列限制为 ID 和 WHO_TYPE_NAME。
实体集本身被暴露,通过:
config.SetEntitySetAccessRule("WHO_TYPES", EntitySetRights.AllRead);
...所以我在设置实体/表访问规则时看不到如何执行此操作。是否可以编写一个 QueryInterceptor 来完成此任务?如果有,怎么做?
我的另一个尝试是编写一个引用自定义对象的自定义方法:
[DataServiceKey("MY_WHO_TYPES")]
public partial class MY_WHO_TYPES
{
public MY_WHO_TYPES() { }
public int MY_WID { get; set; }
public string MY_WNAME { get; set; }
}
我的自定义方法:
[WebGet]
public IQueryable<MY_WHO_TYPES> GetWhoTypesCustom()
{
var whoCustom = from w in this.CurrentDataSource.WHO_TYPES
select new MY_WHO_TYPES() { MY_WID = w.ID, MY_WNAME = w.WHO_TYPE_NAME };
return whoCustom.AsQueryable<MY_WHO_TYPES>();
}
通过此尝试,我收到以下错误消息:
Unable to load metadata for return type 'System.Linq.IQueryable`1[DAL.Models.MY_WHO_TYPES]' of method 'System.Linq.IQueryable`1[DAL.Models.MY_WHO_TYPES] GetWhoTypesCustom()'.
我感觉在响应中出现了某种存储库模式,但我希望这会更简单。
使用元组的suggestion 听起来不错,但我不太确定如何实现它或返回类型是什么:
select new { Tuple<int, string> (w.ID, w.WHO_TYPE_NAME)}; // error: Invalid anonymous type member declarator. Anonymous type members must be declared with a memeber assignment...
接下来我尝试简单地返回一个匿名类型:
[WebGet]
public IQueryable GetWhoTypesCustom()
{
var whoCustom = from w in this.CurrentDataSource.WHO_TYPES
select new { w.ID, w.WHO_TYPE_NAME };
return whoCustom;
}
这个问题是我收到一个错误,表明 IQueryable 类型未定义。
【问题讨论】:
标签: odata wcf-data-services asp.net-4.0 anonymous-types