score:17
Accepted answer
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));
}
score:1
temporary hack that i have used
is to change from
var results = enumerable.empty<t>().asqueryable();
to
var results = queryable.where(o => false);
Source: stackoverflow.com
Related Query
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.DateTime AddSeconds(Double)' method, and this method cannot be translated into
- LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression
- Linq to Entities "does not recognize the method ... method, and this method cannot be translated into a store expression."
- LINQ to Entities does not recognize the method 'Double ToDouble(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- Why the extension method of where for LINQ in this code would print out a single number while it shouldn't print anything at all?
- LINQ to Entities does not recognize the method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String ToString()' method and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.DateTime GetDate()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method ..., and this method cannot be translated into a store expression
- Additional information: LINQ to Entities does not recognize the method and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String ToString()' method,and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression."}
- LINQ to Entities does not recognize the method 'Char get_Chars(Int32)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Single ToSingle(System.String)' method, and this method cannot be translated into a store expression
- Converting linq select into model, LINQ to Entities does not recognize the method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method,and this method cannot be translated into a store expression
- Error LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method Urls.MeaningfulURL, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Boolean HasFlag(System.Enum)' method, and this method cannot be translated into a store expression
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
More Query from same tag
- Linq query equivalent to sql
- How to change some range values in entire list with Linq
- Difference in LINQ query results in .NET 3.5 and 4.5
- Using IQueryable Filtering on a custom collection
- How to write linq query for this SQL query?
- how to extract common part from a string list using C#
- Fastest method to filter XmlDocument xml in C#
- LINQ Select mutiply columns as data source for combobox C#
- The translation of String.IndexOf to SQL does not support versions with a StringComparison argument
- Using Distinct with Linq to SQL
- Create List<CustomObject> from List<string>
- Unity freezes when I apply a LINQ method to a SortedDictionary. What could it be?
- Custom sort logic in OrderBy using LINQ
- Converting F# Quotations into LINQ Expressions
- EF Select records distinct by one field from Entity
- Function that can take different tables and operate on common columns
- unknown column 'extent3.id' in 'where clause'
- How to read NotMapped properties in EF 5?
- ASP.NET MVC: DropDownListFor doesn't select any option
- SQL to entities - complex query selecting dates
- Linq on Custom Class List
- How do I use LINQ to access a specific value from of an array of JObjects that seems to be stored in a string
- How to get parent tables and children tables plus table primary key by retrieve table object properties?
- how to simplify code to print all items in an array use one line code in c#
- How to apply search filtration based on AND condition using LINQ MVC
- C# Linq Join Lambda (a key selector contains the other key selector)
- Converting a given SQL Query to LINQ
- C# List<IEnumerable> get elements in transposed order
- How can I use LINQ with a class that has parallel arrays?
- How can I turn this 12-line method into a 1-line LINQ expression?