简体   繁体   English

来自 EF SQL 的自定义 IQueryable 用于 Azure 移动应用服务

[英]Custom IQueryable from EF SQL for Azure Mobile App Service

I need to have a SQL query added on to my IQueryable in the Azure Mobile App Service backend table controller.我需要在 Azure 移动应用服务后端表 controller 中的IQueryable中添加 SQL 查询。 I need the ItemLibrary table to have a default query as shown.我需要ItemLibrary表有一个默认查询,如图所示。 And either replace the return Query() that is part of the default table controller or merge it with it.并替换作为默认表 controller 一部分的return Query()或将其合并。

This is what I need to do for the GetAllItemLibraries but I can't get it to work:这是我需要为GetAllItemLibraries做的,但我无法让它工作:

public class ItemLibraryController : TableController<ItemLibrary>
{
    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);

        MIIToolsContext context = new MIIToolsContext();
        DomainManager = new EntityDomainManager<ItemLibrary>(context, Request, enableSoftDelete: true);
    }

    // GET tables/ItemLibrary
    public IQueryable<ItemLibrary> GetAllItemLibraries()
    {
        using (MIIToolsContext context = new MIIToolsContext())
        {
            string sqlQueryString = "SELECT * FROM dbo.ItemSpecification WHERE Id IN (SELECT DISTINCT EquipmentItemID FROM dbo.SiteEquipment WHERE EquipmentSiteID = '8FA79274-C5CC-4610-9D6E-A7062D3CE966')";
            string test = "SELECT TOP 10 * FROM dbo.ItemSpecification";

            return context.ItemLibrary.SqlQuery(sqlQueryString).AsQueryable();
            // return context.ItemLibrary.SqlQuery(test).AsQueryable();
        }

        // possibly merge the SQL IQueryable with the default query?
        // Query.Union([sqlquery])

        // default query
        // return Query();
    }
}

I could do the query in Linq and not SQL, but I have no idea how to reference the SiteEquipment table from the context of the ItemLibrary table controller.我可以在 Linq 而不是 SQL 中进行查询,但我不知道如何从ItemLibrary SiteEquipment I need that to be the default query but still have it tack on any OData from a client.我需要将其作为默认查询,但仍需要将其添加到客户端的任何 OData 上。

For those of you who don't know anything about Azure Mobile App Service and want to comment here is an example of the out of the box default table controller wrapper:对于那些对 Azure 移动应用服务一无所知并想在此处发表评论的人,这里是开箱即用的默认表 controller 包装器的示例:

public class ExampleController : TableController<Example>
{
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            MobileServiceContext context = new MobileServiceContext();
            DomainManager = new EntityDomainManager<Example>(context, Request, enableSoftDelete: true);
        }

        // GET tables/Example
        public IQueryable<Example> GetAllExample()
        {
            return Query();
        }

        // GET tables/Example/48D68C86-6EA6-4C25-AA33-223FC9A27959
        public SingleResult<Example> GetExample(string id)
        {
            return Lookup(id);
        }

        // PATCH tables/Example/48D68C86-6EA6-4C25-AA33-223FC9A27959
        public Task<Example> PatchExample(string id, Delta<Example> patch)
        {
            return UpdateAsync(id, patch);
        }

        // POST tables/Example
        public async Task<IHttpActionResult> PostExample(Example item)
        {
            Example current = await InsertAsync(item);
            return CreatedAtRoute("Tables", new { id = current.Id }, current);
        }

        // DELETE tables/Example/48D68C86-6EA6-4C25-AA33-223FC9A27959
        public Task DeleteExample(string id)
        {
            return DeleteAsync(id);
        }
}

Don't know if this is the most elegant way but it is working as needed:不知道这是否是最优雅的方式,但它可以根据需要工作:

public IQueryable<ItemLibrary> GetAllItemLibrarys(string site)
        {
            using (MIIToolsContext context = new MIIToolsContext())
            {
                //create sql param for site param
                var siteParam = new SqlParameter("@Site", site);

                //use sql to access siteequipment id's by current project site
                var items = context.ItemLibrary.SqlQuery("Select * from dbo.MIIToolsItemLibrary where Id in (SELECT DISTINCT ItemLibraryID FROM dbo.MIIToolsEquipment WHERE ProjectSiteID = @Site)", siteParam).Select(i => i.Id).ToList();

                //use linq on local tablecontroller generated query against sql id results
                return Query().Where(i => items.Any(a => a == i.Id));
            }

            //not using default query
            //return Query();
        }

Since I didn't know how (or the best way) to access the SiteEquipment linq query object I just used sql for that part and then was able to insert the result into the provided ItemLibrary Query so I can still run odata queries against it.由于我不知道如何(或最好的方法)访问 SiteEquipment linq 查询 object 我只是使用 sql 作为该部分,然后能够将结果插入到提供的 ItemLi 查询中。

I also added a "site" parameter so the mobile client applications are only dealing with items related to that project site.我还添加了一个“站点”参数,因此移动客户端应用程序只处理与该项目站点相关的项目。 just what I wanted.正是我想要的。

I have test it, I think your query string doesn't work because of EntityData .我已经测试过了,我认为你的查询字符串因为EntityData而不起作用。 So I changed your test string and test it.所以我改变了你的test字符串并测试它。

It works!有用!

在此处输入图像描述

public IQueryable<ItemLibrary> GetAllItemLibrarys()
{
        using (ZUMOAPPNAMEContext context = new ZUMOAPPNAMEContext())
        {
            string sqlQueryString = "Select * from dbo.ItemSpecification where Id in (SELECT DISTINCT EquipmentItemID FROM dbo.SiteEquipment WHERE EquipmentSiteID = '8FA79274-C5CC-4610-9D6E-A7062D3CE966')";
            string test = "Select TOP 10 * from dbo.ItemSpecification";
            // Due to EntityData, so sql like below
            test = @"Select TOP 10 *,'111' Id,0x0000000000000FA4 Version,null CreatedAt,null UpdatedAt,CAST(COALESCE(NULL,0) AS BIT) as Deleted from dbo.ItemSpecification";
            var a = context.ItemLibrary.SqlQuery(test).ToList().AsQueryable();
            return a;
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM