我正在使用Azure MobileServices编写WindowsPhone应用程序,并希望使用TableControllers将数据库公开给客户端。
public IQueryable<Activity> GetAllActivities()
{
return Query();
}
但是如何过滤控制器返回客户端的数据呢? 我已经发现,我可以使用Where(..)来过滤行。
但是有没有办法从结果集中排除列?
提前致谢!
答案 0 :(得分:1)
有两种方法可以做到,但取决于要求:
1 - 使用与模型映射的特定dtos
您可以创建与Azure Mobile Service中的模型映射的Dtos,并且模型与数据库连接。 Dtos不需要公开我的所有模型,比如
我可以
public class MyDto
{
public string Name {get;set;}
}
我的模特可以
public class MyModel
{
public string Name {get;set;}
public string Details {get;set;}
}
然后需要映射它们,在Azure Mobile Service中我们通常使用AutoMapper
2 - 使用Linq中的“选择”方法
在客户端应用程序中,您可以执行类似
的操作_client.GetTable<YourObject>().Select(x => new YourObject()
{
Prop1 = x.Prop1,
Prop2 = x.Prop2,
}
当您需要进行查询时,您应该使用Linq。
答案 1 :(得分:0)
one good option for you would be overwriting your EntityDomainManager.
What I mean is creating a new class call it for example KhosroItemDomainManager
public class KhosroItemDomainManager : MappedEntityDomainManager<YourModelHere, YourEntityHere>
{
public KhosroItemDomainManager(DbContext context,
System.Net.Http.HttpRequestMessage request, ApiServices services): base(context, request, services)
{ }
public override IQueryable<YourModelHere> Query()
{
MobileServiceContext ctx = this.Context as MobileServiceContext;
var items = from ai in ctx.YourModelHere
select new AuctionItem
{
Name = ai.Name,
Description = ai.Description,
};
return items;
}
public override System.Web.Http.SingleResult<YourModelHere> Lookup(string id)
{
var ctx = Context as MobileServiceContext;
var item = from ai in ctx.AuctionItems
where ai.Id == id
select new AuctionItem
{
Id = ai.Id,
Description = ai.Description,
};
return new System.Web.Http.SingleResult<YourModelHere>(item);
}
public override Task<bool> DeleteAsync(string id)
{
return base.DeleteItemAsync(id);
}
public override Task<YourModelHere> UpdateAsync(string id, System.Web.Http.OData.Delta<YourModelHere> patch)
{
return base.UpdateEntityAsync(patch, id);
}
}
as simple as that, basically, all you need to do is creating your own domain manager. and override the Query, Lookup , DeleteAsync and UpdateAsync
after that you need to go back to your Controller and replace this part with
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MobileServiceContext context = new MobileServiceContext();
DomainManager = new EntityDomainManager<YourModelHere>(context, Request, Services);
}
with
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MobileServiceContext context = new MobileServiceContext();
DomainManager = new KhosroItemDomainManager(context, Request, Services);
}