Enumerable.Empty< T>().AsQueryable();此方法支持LINQ to Entities基础结构,不能直接在您的代码中使用 [英] Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code

查看:301
本文介绍了Enumerable.Empty< T>().AsQueryable();此方法支持LINQ to Entities基础结构,不能直接在您的代码中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到运行时错误

此方法支持LINQ to Entities基础结构,但不支持 旨在直接从您的代码中使用.

This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code.

描述:执行以下操作时发生未处理的异常 当前的Web请求.请查看堆栈跟踪以获取更多信息 有关错误及其在代码中起源的信息.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

异常详细信息:System.InvalidOperationException:此方法 支持LINQ to Entities基础结构,并非旨在 直接从您的代码中使用.

Exception Details: System.InvalidOperationException: This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code.

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

I am trying to produce query that rather than filtering on every search criteria would include by adding all matching records on all search fields (would do OR rather than 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;
}

当我引入var results = Enumerable.Empty<T>().AsQueryable();时,机制开始失败,我需要从空白开始.

Mechanism started failing when I introduced var results = Enumerable.Empty<T>().AsQueryable(); which I need to start from something empty.

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

推荐答案

您可以通过仅合并具有的结果来重构代码以不需要空集:

you can refactor the code to not need an empty set by only ever unioning results that you have:

public static IQueryable<T> ApplySearch<T>(this IQueryable<T> queryable, SearchModel search) where T : class 
{
    var subQueries = new List<IQueryable<T>>();
    if (search != null)
    {
        if (search.PolicyNumber.HasValue && typeof (IPolicyNumber).IsAssignableFrom(queryable.ElementType))
        {
            subQueries.Add(queryable.SearchByPolicyNumber(search));
        }

        if (search.UniqueId.HasValue && typeof (IUniqueId).IsAssignableFrom(queryable.ElementType))
        {
            subQueries.Add(queryable.SearchByUniqueId(search));
        }

        if (!string.IsNullOrWhiteSpace(search.PostCode) && typeof(IPostCode).IsAssignableFrom(queryable.ElementType))
        {
            subQueries.Add(queryable.SearchByPostCode(search));
        }
    }

    return subQueries.DefaultIfEmpty(queryable)
        .Aggregate((a, b) => a.Union(b));
}

这篇关于Enumerable.Empty&lt; T&gt;().AsQueryable();此方法支持LINQ to Entities基础结构,不能直接在您的代码中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆