score:2
you can assign a variable for _ratingsprovider.getrating
first, to avoid the error in the title.
after that you will have another error saying entity framework does not support your query. you need something like linqkit https://github.com/scottksmith95/linqkit. asexpandable()
and invoke(x, id)
are from linqkit.
using linqkit;
public iqueryable<tuple<document, rating>> getdocumentswithrating(iqueryable<document> documents, int id)
{
var getratingexpression = _ratingsprovider.getrating;
return documents.asexpandable().select(x => new
{
document = x,
rating = getratingexpression.invoke(x, id)
}).asenumerable()
.select(x => new tuple<document, rating>(x.document, x.rating)).asqueryable().where(x.item2 == rating.ok);
}
answer to your second question:
public expression<func<document, int, rating>> getrating
{
get
{
return (document, id) =>
document.calculations.where(x => x.cid == id).any() ?
document.calculations
.where(x => x.cid == id)
.select(x => x.rating.hasvalue ? x.rating.value : rating.none)
.single()
rating.unknown;
}
}
answer to the .asenumerable() part of your second question: i guess the problem you had came from the use of tuple<document, rating>, try to define a class
public class documentrating
{
public document { get; set; }
public rating { get; set; }
}
public iqueryable<documentrating> getdocumentswithrating(iqueryable<document> documents, int id)
{
var getratingexpression = _ratingsprovider.getrating;
return documents.asexpandable().select(x => new documentrating
{
document = x,
rating = getratingexpression.invoke(x, id)
})
.where(x.item2 == rating.ok);
}
Source: stackoverflow.com
Related Query
- Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[]' using linq lambda expression
- Linq cast conversion Xelement error: Unable to cast object of type 'System.Xml.Linq.XElement' to type 'System.IConvertible'
- Unable to cast object of type 'WhereSelectListIterator` in LINQ
- LINQ Issue: Unable to cast object of type 'System.Reflection.Module' to type 'System.Reflection.Emit.ModuleBuilder
- Removing duplicates using LINQ returns "Error removing duplicates: Unable to cast object of type 'System.Int32' to type 'System.String'."
- LINQ with String.Join() error: "System.InvalidCastException: Unable to cast object of type '<DistinctIterator>
- linq query getting Unable to cast object of type 'System.Boolean' to type 'System.String'
- Unable to cast object of type 'System.Double' to type 'System.String' linq query
- Unable to cast object of type 'System.Int32' to type 'System.String' in LINQ query
- Assigning LINQ Results to child Property - InvalidCastException: Unable to cast object of type
- LINQ Error unable to cast object of type 'system.datetime' to type 'system.string'
- Unable to cast object of type in below Linq method
- Unable to cast object of type grouping to datarow after i grouped datatable with linq
- Linq query error - Unable to cast object of type 'V1.Controllers.Loan' to type 'System.IConvertible
- Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.MemberExpression'
- Unable to cast object of type WhereSelectListIterator 2 System.Collections.Generic.List
- Unable to cast object of type 'System.Data.Linq.DataQuery`1[System.Int32]' to type 'System.IConvertible'
- Unable to cast object of type 'WhereEnumerableIterator`1' to type 'System.Collections.Generic.ICollection`1
- Unable to cast object of type 'System.Collections.Generic.List`1[System.Decimal]' to type 'System.IConvertible'
- Unable to cast object of type 'System.Int32' to type 'System.String'
- Unable to cast a object of type 'SZArrayEnumerator' to type 'System.Collections.Generic.IEnumerator' 1[System.String]'
- Unable to cast object of type WhereSelectListIterator
- Unable to cast object of type 'System.Linq.EnumerableQuery to type 'Microsoft.Azure.Documents.Linq.IDocumentQuery
- Unable to cast object of type System.Func`2 with Int64
- Unable to cast object of type '<>f__AnonymousType5`6
- Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.Data.IDataReader'
- Unable to cast object of type 'System.Int16' to type 'System.String'
- Unable to cast object of type 'System.Data.Objects.ObjectQuery`1 [Manager.Data.Channel]' to type 'Manager.Data.Channel'
- Unable to cast the type X to type Y. LINQ to Entities only supports casting EDM primitive or enumeration types
- Unable to cast object of type 'WhereSelectEnumerableIterator
More Query from same tag
- removing items from a list based on two properties
- linq query to parse my class
- LINQ Join usage data from two data sets into one
- is it possible to have a conditional field in an anonymous type
- byte[] does not contain a definition for SequenceEqual authentication method
- Query only the first element in C# ObjectQuery like LIMIT = 1 in SQL
- LINQ from SortedList C#
- Filtering a c# collection by multiple parameters where some may be null
- How To Join Many Tables In Linq Lambda Expressions?
- Data Access from Entity framework works during debugging but not on live
- LINQ Query to remove duplicate items in a List based on the value
- Algorithm to create batches of Files
- Getting all elements concatenate in a string with LINQ
- F# Power Pack Linq Issue
- Confused on c# lambda expression with Linq
- How to populate a dictionary from another with same keys but values cumulated
- How to write a LINQ query or Lambda expression on one to many relation with filter
- Cartesian Product for Group members
- Inserting new XML Data at specific Point in document C# Using LINQ
- Create a tab delimited string from a list of object
- MVC Linq Query with dynamic column name in WHERE clause
- How do you use LINQ to find the duplicate of a specific property?
- How to get max/min of list of 2d array in java
- Only parameterless constructors and initializers are supported in LINQ to Entities
- Can we apply the transaction scope for entire API actions on 1 request
- What is LINQ equivalent of SQL’s "IN" keyword
- Linq order by parent and child
- How to do this query in Linq?
- How to Union child collections from their parent entities in EF?
- Expressing IN sql statement in Linq with computed string in where clause