C# 从OData端点检索行的子集

C# 从OData端点检索行的子集,c#,asp.net-mvc,linq,asp.net-web-api,odata,C#,Asp.net Mvc,Linq,Asp.net Web Api,Odata,我有一个ASP.NET Web API 2公开了OData端点 在前端,我使用ASP.NETMVC5。为了使用端点,我使用了WCF服务引用 我面临的问题是,我需要从给定ID列表的端点检索行的子集 这是我在OData端点中使用的实体 class MyEntity { public int ID {get; set;} public string Name {get; set;} public int Age {get; set;} } 使用LINQ,我在其他情况下使用以下语句解决了这

我有一个ASP.NET Web API 2公开了OData端点

在前端,我使用ASP.NETMVC5。为了使用端点,我使用了WCF服务引用

我面临的问题是,我需要从给定ID列表的端点检索行的子集

这是我在OData端点中使用的实体

class MyEntity
{
  public int ID {get; set;}
  public string Name {get; set;}
  public int Age {get; set;}
}
使用LINQ,我在其他情况下使用以下语句解决了这个问题

var result = entitiesContext.MyEntity
  .Where(x => idsEmployees.Select(y => y).Contains(x.ID));
其中IDSSemployees是我需要检索的员工ID的列表

在当前场景中使用此语句,我得到以下异常:

将Linq表达式转换为URI时出错:方法“Contains”不可用 支持

我怎样才能解决这个问题


谢谢。

经过一番挖掘,我在这个博客中找到了一个解决方案

这里有一个解决问题的扩展方法

/// <summary>
/// Retrieves the subset of entities in a table exposed through OData
/// </summary>
/// <typeparam name="T">Entity type</typeparam>
/// <typeparam name="U">Subset type</typeparam>
/// <param name="query"></param>
/// <param name="Set"></param>
/// <param name="propertyExpression"></param>
/// <returns></returns>
public static DataServiceQuery<T> SubSet<T, U>(
    this DataServiceQuery<T> query, 
    IEnumerable<U> Set, 
    Expression<Func<T, U>> propertyExpression)
{
    //The Filter Predicate that contains the Filter criteria
    Expression filterPredicate = null;
    //The parameter expression containing the Entity Type
    var param = propertyExpression.Parameters.Single();
    //Get Key Property 
    //The Left Hand Side of the Filter Expression
    var left = propertyExpression.Body;
    //Build a Dynamic Linq Query for finding an entity whose ID is in the list
    foreach (var id in Set)
    {
        //Build a comparision expression which equats the Id of the Entity with this value in the IDs list
        // ex : e.Id == 1
        Expression comparison = Expression.Equal(left, Expression.Constant(id));
        //Add this to the complete Filter Expression
        // e.Id == 1 or e.Id == 3
        filterPredicate = (filterPredicate == null) 
            ? comparison 
            : Expression.Or(filterPredicate, comparison);
    }

    //Convert the Filter Expression into a Lambda expression of type Func<Lists,bool>
    // which means that this lambda expression takes an instance of type EntityType and returns a Bool
    var filterLambdaExpression = 
        Expression.Lambda<Func<T, bool>>(filterPredicate, param);
    return (DataServiceQuery<T>)query.Where<T>(filterLambdaExpression);
}
//
///检索通过OData公开的表中的实体子集
/// 
///实体类型
///子集类型
/// 
/// 
/// 
/// 
公共静态DataServiceQuery子集(
此DataServiceQuery查询,
IEnumerable集合,
表达式属性(表达式)
{
//包含筛选条件的筛选谓词
表达式filterPredicate=null;
//包含实体类型的参数表达式
var param=propertyExpression.Parameters.Single();
//获取密钥属性
//过滤器表达式的左侧
var left=propertyExpression.Body;
//构建动态Linq查询,以查找ID位于列表中的实体
foreach(集合中的变量id)
{
//构建一个比较表达式,该表达式将实体的Id与Id列表中的该值相等
//例:e.Id==1
表达式比较=表达式.Equal(左,表达式.Constant(id));
//将其添加到完整的筛选器表达式中
//e.Id==1或e.Id==3
filterPredicate=(filterPredicate==null)
?比较
:表达式。或(筛选预测、比较);
}
//将筛选器表达式转换为Func类型的Lambda表达式
//这意味着这个lambda表达式接受EntityType类型的实例并返回Bool
变量filterLambdaExpression=
Lambda(filterPredicate,param)表达式;
return(DataServiceQuery)query.Where(filterLambdaExpression);
}
下面是使用它的方法

var result = entitiesContext.MyEntity.Subset<MyEntity, int>(idsEmployees, x => x.ID);
var result=entitiesContext.MyEntity.Subset(idsEmployees,x=>x.ID);

$filter
可能是您想要的,但我不确定(这个问题让人有些困惑)OData v4是不是?从客户端筛选?@octavioccl是