C#WebApi Odata - EF不过滤

时间:2017-01-27 20:05:41

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

大家好!

在sql server返回后进行ODATA过滤,我想先过滤并仅返回我的记录。

我有一张带有+ 1Milion记录的表格。

我需要中止OData的使用吗? (OMG)...

我的系统开发中有超过60%已经完成并且全部使用OData ...(类似于300小时......令人沮丧......)

这可能是一个错误,或者我正在做一些疯狂的事情?

受影响的装配

.NET Framework 4.5.2

Microsoft.Data.OData 5.6.0

System.Web.Http.OData 5.3.1.0

EntityFramework 6.0.0.0

重现步骤

我有一个名为Project的实体,如下所示:

    function onOpen(e) {
             var menu = SpreadsheetApp.getUi().createAddonMenu(); 
              var vendorsheet = SpreadsheetApp.openById('1Bt4s9aOfjkCyZRvHZMjMdntgID2VYF7Qzmjc7Z7YP1E')

   for(var i = 2; i < 5; i++){
    var j = 4;
   menu.addItem(String(vendorsheet.getRange('A'+ String(i)).getValue()),'Test')

     if(i = j){
      menu.addItem(String(vendorsheet.getRange('A'+ String(i)).getValue()),'Test')
     } else { 
       menu.addItem(String(vendorsheet.getRange('A'+  String(i)).getValue()),'Test')
   }
   }



   menu.addToUi();
 }

我正在使用以下方法过滤此ODATA:

http://localhost:36983/odata/Project $滤波器= IdProject +当量+ 1527&安培; $选择= IdProject,名称

我的控制器:

f = f';
tMat = blkdiag(f{:})+(blkdiag(f{:}))';
y2BB = [reshape(cell2mat(v)',1,N*M);...
        cell2mat(reshape(b',1,M*N));...
        reshape(diag(blkdiag(f{:})),K,N*M);...
        reshape(tMat((tril(tMat,-1)~=0)),sum(1:K-1),M*N)]; 
y2YBar = [ones(T,1),data,data.^2];

jj=1;
kk=1;
ll=1;
for k=1:sum(1:K-1)
    y2YBar = [y2YBar,data(:,jj).*data(:,kk+jj)];
    if kk<(K-ll)
        kk=kk+1;
    else
        kk=1;
        jj=jj+1;
        ll=ll+1;
    end
end
y = y2YBar*y2BB;

ODATA响应:

[Table("Project")]
public partial class Project
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Project()
    {
        Batches = new HashSet<Batch>();
    }

    [Key]
    public int IdProject { get; set; }

    public int IdCompany { get; set; }

    [StringLength(100)]
    public string Name { get; set; }

    public virtual Company Company { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Batch> Batches { get; set; }
}

预期结果

public class ProjectsController : ODataController
{
    private IProtocolContext db = new IProtocolContext();

    public ProjectsController()
    {
        db.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
    }

    // GET: odata/Projects
    [EnableQuery]
    public PageResult<Project> GetProjects()
    {
        var properties = Request.ODataProperties();
        return new PageResult<Project>(db.Project, properties.NextLink, properties.TotalCount);
    }

    //...
}

实际结果

我的调试从实体框架返回了这个sql:

{
  "odata.metadata":"http://localhost:36983/odata/$metadata#Projects&$select=IdProject,Name","odata.count":"1","value":[
    {
      "IdProject":1527,"Name":"Piloto - TSI 2006"
    }
  ]
}

其他细节

这导致我出现性能问题

1 个答案:

答案 0 :(得分:3)

我看起来像这样。请注意ODataQueryOptions的使用。

[HttpGet, Route("getPagedData")]
public PageResult<MyEFClassToFilterOn> GetPagedData(ODataQueryOptions opts)
{
     using (var db = new dbEntities())
     {
        var myQueryable = _searchRepo.GetBatchQueryable(db);
        var myFilteredQueryable = opts.ApplyTo(myQueryable.AsQueryable()) as IQueryable<MyEFClassToFilterOn>;
        var result = myFilteredQueryable.ToList();
        var rowcount = result.Count();
        return new PageResult<MyEFClassToFilterOn>(emailBatch, null, rowcount);
     }
}

我原来的网址看起来像这样: http://localhost:59637/api/mycontroller/getPagedData/ $ inlinecount =所有页&安培; $滤波器=(substringof(%27test%27,tolower的(姓)))%20or%20(substringof(%27test%27,tolower的(名字)))及$跳过= 0&安培; $顶部= 10

希望有所帮助!