相关疑难解决方法(0)

Enumerable.Empty <T>()AsQueryable已(); 此方法支持LINQ to Entities基础结构,不应在代码中直接使用

我收到运行时错误

此方法支持LINQ to Entities基础结构,不应在代码中直接使用.

描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

异常详细信息:System.InvalidOperationException:此方法支持LINQ to Entities基础结构,不应在代码中直接使用.

我试图通过在所有搜索字段上添加所有匹配记录(OR而不是AND)来生成查询而不是对每个搜索条件进行过滤.

public static IQueryable<T> ApplySearch<T>(this IQueryable<T> queryable, SearchModel search) where T : class 
{
    var results = Enumerable.Empty<T>().AsQueryable();
    if (search != null)
    {
        if (search.PolicyNumber.HasValue && typeof (IPolicyNumber).IsAssignableFrom(queryable.ElementType))
        {
            results = results.Union(queryable.SearchByPolicyNumber(search));
        }

        if (search.UniqueId.HasValue && typeof (IUniqueId).IsAssignableFrom(queryable.ElementType))
        {
            results = results.Union(queryable.SearchByUniqueId(search));
        }

        if (!string.IsNullOrWhiteSpace(search.PostCode) && typeof(IPostCode).IsAssignableFrom(queryable.ElementType))
        {
            results = results.Union(queryable.SearchByPostCode(search));
        }
    }

    return results;
}
Run Code Online (Sandbox Code Playgroud)

当我介绍var results = Enumerable.Empty<T>().AsQueryable();我需要从空的东西开始时机制开始失败.

如何从空集开始,然后在顶部构建Linq-to-sql结果?

c# linq entity-framework iqueryable linq-to-sql

8
推荐指数
1
解决办法
4330
查看次数

标签 统计

c# ×1

entity-framework ×1

iqueryable ×1

linq ×1

linq-to-sql ×1