Web API OData对$ select查询选项的无类型支持

时间:2013-11-26 21:04:54

标签: asp.net-web-api-odata

我正在使用ASP.NET Web API 2 OData中的新无类型支持。我有兴趣为 $ select 查询选项提供支持。如何省略未被$ select查询选项选择的EdmEntityObject的结构属性?

以下是使用无类型模型的一个非常简单的示例的Web API配置示例。

public static IEdmModel BuildEdmModel()
{
    var model = new EdmModel();
    var container = new EdmEntityContainer("Model", "OData");
    var product = new EdmEntityType("Model", "Product");
    var productKey = product.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Guid);
    product.AddKeys(productKey);
    product.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
    product.AddStructuralProperty("Price", EdmPrimitiveTypeKind.Double);
    model.AddElement(product);
    model.AddElement(container);
    model.SetIsDefaultEntityContainer(container, true);
    container.AddEntitySet("Products", product);

    return model;
}

public static void Register(HttpConfiguration config)
{
    config.Routes.MapODataRoute("ODataRoute", "odata", BuildEdmModel());
}

以下是来自简单ODataController的部分片段

public EdmEntityObjectCollection Get()
{
    var path = Request.GetODataPath();
    var edmType = path.EdmType;
    var collectionType = edmType as IEdmCollectionType;
    var entityType = collectionType.ElementType.AsEntity();
    var entitySetName = entityType.EntityDefinition().Name;
    var model = Request.GetEdmModel();
    var queryContext = new ODataQueryContext(Request.GetEdmModel(), entityType.Definition);
    var queryOptions = new ODataQueryOptions(queryContext, Request);

    return GetData(Request.GetEdmModel(), queryOptions);
}

public static EdmEntityObjectCollection GetData(IEdmModel edmModel, ODataQueryOptions queryOptions)
{
    var selectedPropertyNames = new string[0];
    // determine the selected property names
    if (queryOptions.SelectExpand != null && queryOptions.SelectExpand.SelectExpandClause != null && (!queryOptions.SelectExpand.SelectExpandClause.AllSelected || queryOptions.SelectExpand.SelectExpandClause.SelectedItems.OfType<WildcardSelectItem>().Any()))
    {
        selectedPropertyNames = queryOptions.SelectExpand.RawSelect.Split(',');
    }    

    // TODO: Now that we have the selected properties, how do I remove the structural properties from the EdmEntityObject that were not selected by the $select query option?

    var productSchemaType = edmModel.FindDeclaredType(string.Format("{0}.Product", "Model"));
    var productEntityType = productSchemaType as IEdmEntityType;
    var productEntityTypeReference = new EdmEntityTypeReference(productEntityType, true);
    var products = new EdmEntityObjectCollection(new EdmCollectionTypeReference(new EdmCollectionType(productEntityTypeReference), true));

    var productWindows = new EdmEntityObject(productEntityTypeReference);
    productWindows.TrySetPropertyValue("ID", new Guid("52D811A0-9065-4B83-A2E8-0248FBA9FBF5"));
    productWindows.TrySetPropertyValue("Name", "Microsoft Windows 8");
    productWindows.TrySetPropertyValue("Price", 179.99);

    var productOffice = new EdmEntityObject(productEntityTypeReference);
    productOffice.TrySetPropertyValue("ID", new Guid("CB39EBD0-4751-4D5F-A76C-78FCC7A9CE1A"));
    productOffice.TrySetPropertyValue("Name", "Microsoft Office 2013");
    productOffice.TrySetPropertyValue("Price", 399.99);

    products.Add(productWindows);
    products.Add(productOffice);

    return products;
}

这将输出:

{
  "odata.metadata":"http://localhost:59511/odata/$metadata#Products","value":[
    {
        "ID":"52d811a0-9065-4b83-a2e8-0248fba9fbf5","Name":"Microsoft Windows 8","Price":179.99
    },{
        "ID":"cb39ebd0-4751-4d5f-a76c-78fcc7a9ce1a","Name":"Microsoft   Office 2013","Price":399.99
    }
  ]
}

如果用户应用$ select查询选项,例如 / odata / Products?$ select = Name 。这应该导致以下输出:

{
  "odata.metadata":"http://localhost:59511/odata/$metadata#Products","value":[
    {
      "Name":"Microsoft Windows 8"
    },{
      "Name":"Microsoft Office 2013"
    }
  ]
}

非常感谢任何帮助

3 个答案:

答案 0 :(得分:2)

以下适用于我。

var oDataProperties = Request.ODataProperties()
oDataProperties.SelectExpandClause = queryOptions.SelectExpand.SelectExpandClause;

扩展功能位于System.web.OData.dll v5.2.0.0。

答案 1 :(得分:0)

这是我的代码。

private void ApplySelectExpand(SelectExpandQueryOption selectExpand)
    {
        if (selectExpand != null)
        {
            Request.SetSelectExpandClause(selectExpand.SelectExpandClause);
        }
    }

答案 2 :(得分:-1)

如果我理解你的问题,有两种方法可以解决你的问题。

  1. 在IQueryable结果上调用ODataQueryOptions的ApplyTo方法 return queryOptions.ApplyTo(products);

  2. 在GetData方法上添加属性Queryable,让WebAPI处理查询选项
    [Queryable]
    public static EdmEntityObjectCollection GetData(IEdmModel edmModel) {...}