将DAL对象映射到DTO对象时,我得到一个意外的查询。
我有一些DAL对象:
public class MainDalObject { public Guid Id { get; set;} public string Description { get; set; } public ICollection<SubDalObject> Subs { get; set; } } public class SubDalObject { public Guid Id { get; set;} public string Description { get; set; } public MainDalObject Main { get; set; } }我的DTO课程:
public class MainObject { public Guid Id { get; set;} public string Name { get; set; } public IEnumerable<SubObject> Subs { get; set; } } public class SubObject { public Guid Id { get; set;} public string Name { get; set; } public MainDalObject Main { get; set; } }我的MainObject控制器包含一个IQueryable方法:
public IQueryable<MainObject> Get() { return (from m in Context.Get<MainDalObject>() select new MainObject { Id = m.Id, Name = m.Description, Subs = m.Subs.Select(s => new SubObject { Id = s.Id, Name = s.Name } }); }这工作正常,但查询不是最佳的。 当我在/ api / MainObject上触发查询时,我根本没有选择子项。 但是当我查看查询时,无论如何它都在选择子项。
但是,当我将查询更改为/ api / MainObject?$ select = Id,Name时,查询未选择SubObjects。
所以我期待的是WebApi框架中的某个地方,当没有使用SelectExpandFilter时,响应编写器正在执行ToList(),而不指定Select语句。
我正在寻找解决这个问题的最佳位置,我可能会设置一个选择扩展ODataQueryOption,它可能会伪造一个选择或扩展调用,但我不确定这是否可行。
When mapping a DAL object to DTO object I get an unexpected query.
I have some DAL objects:
public class MainDalObject { public Guid Id { get; set;} public string Description { get; set; } public ICollection<SubDalObject> Subs { get; set; } } public class SubDalObject { public Guid Id { get; set;} public string Description { get; set; } public MainDalObject Main { get; set; } }And my DTO classes:
public class MainObject { public Guid Id { get; set;} public string Name { get; set; } public IEnumerable<SubObject> Subs { get; set; } } public class SubObject { public Guid Id { get; set;} public string Name { get; set; } public MainDalObject Main { get; set; } }My MainObject controller contains a IQueryable method:
public IQueryable<MainObject> Get() { return (from m in Context.Get<MainDalObject>() select new MainObject { Id = m.Id, Name = m.Description, Subs = m.Subs.Select(s => new SubObject { Id = s.Id, Name = s.Name } }); }This works fine, however the query is not optimal. When i trigger the query on /api/MainObject , I am not selecting the subitems at all. But when i look at the query it is selecting the subItems anyway.
However when I change the query to /api/MainObject?$select=Id,Name , the query is not selecting the SubObjects.
So what I am expecting is that somewhere in the WebApi framework, when no SelectExpandFilter is used, the responsewriter is doing a ToList(), without specifying the Select statement.
I am looking for the best place to fix this problem, I could probably set a select expand ODataQueryOption that could fake an select or expand call, but I am not sure if that is the way to go.
最满意答案
如果其他人遇到此问题,这就是我为解决这个问题所做的工作。 select扩展查询应该从请求上下文中的edmmodel派生,但这基本上是它的工作原理。
public virtual IQueryable<TDTO> Get(ODataQueryOptions queryOptions) { if (queryOptions.SelectExpand == null) { var selectOption = new SelectExpandQueryOption("Id,Name", string.Empty, queryOptions.Context); Request.SetSelectExpandClause(selectOption.SelectExpandClause); } return (from m in Context.Get<MainDalObject>() select new MainObject { Id = m.Id, Name = m.Description, Subs = m.Subs.Select(s => new SubObject { Id = s.Id, Name = s.Name } }); }If someone else runs in this problem, this is what I have done to fix it. The select expand query should be derived from the edmmodel in the request context, but this is basically how it works.
public virtual IQueryable<TDTO> Get(ODataQueryOptions queryOptions) { if (queryOptions.SelectExpand == null) { var selectOption = new SelectExpandQueryOption("Id,Name", string.Empty, queryOptions.Context); Request.SetSelectExpandClause(selectOption.SelectExpandClause); } return (from m in Context.Get<MainDalObject>() select new MainObject { Id = m.Id, Name = m.Description, Subs = m.Subs.Select(s => new SubObject { Id = s.Id, Name = s.Name } }); }更多推荐
发布评论