Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code

I am getting runtime error

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

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.

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

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;
}

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

How do I start out from an empty set and then build Linq-to-sql results on top?

like image 769
Matas Vaitkevicius Avatar asked Jun 14 '16 14:06

Matas Vaitkevicius


People also ask

What is AsQueryable LINQ?

AsQueryable(IEnumerable) Converts an IEnumerable to an IQueryable. AsQueryable<TElement>(IEnumerable<TElement>) Converts a generic IEnumerable<T> to a generic IQueryable<T>.

Why do we use AsQueryable () on collection?

AsQueryable is a method that allow the query to be converted to an instance of IQueryable. When you use AsQueryable operator on an existing query and apply further transformations such as applying a filter or specifying a sort order, those lambda statements are converted to Expression trees.

Which method is valid in LINQ?

Most of the LINQ projection and restriction methods are supported in LINQ to Entities queries, with the exception of those that accept a positional argument. For more information, see Standard Query Operators in LINQ to Entities Queries.

What is enumerable empty?

IEnumerable<T> Enumerable. Empty<T>(); From MSDN: The Empty(TResult)() method caches an empty sequence of type TResult . When the object it returns is enumerated, it yields no elements.


1 Answers

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));
}
like image 179
Servy Avatar answered Oct 26 '22 12:10

Servy