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
- Is it necessary not to use methods in LINQ queries?
- Include Path Expression Must Refer To A Navigation Property
- Linq repeat creation of empty object?
- Linq get all data from specific date, and if data + 1 day is another data and so on
- Remote Attribute not allowing Post
- Remove all records with opposite sign
- Update List from another List with Matching values using Linq
- C# Array.Contains () compilation error
- Check if a string is sorted
- improve this Linq with 2 Selects to same table
- ParameterExpression of type 'MyClass' cannot be used for delegate parameter of type 'System.Object'
- Dynamic EF Where Clause raising ArgumentNullException
- How to build () => new { x.prop} lambda expression dynamically?
- How to change NOCOUNT setting within LinqToSQL because of ChangeConflictException in Linq to Sql update
- Is there a way in linq wherin i can insert a row(from dictionary) in datatable using the list of column names c#?
- Why does this LINQ statement return null and not a IEnumerable with count=0
- Which is a better linq query? .Where(crit).FirstOrDefault() or .Where(someCrit).FirstOrDefault(someOtherCrit)?
- Is there Faster way to REGEX a SortedList?
- How to select all the values of an object's property on a list of typed objects in .Net with C#
- LINQ - get parent based on last child condition
- Return TRUE for ALL when specific property is smaller then other property for both lists
- LINQ to XML & bind to Gridview
- List of object - combining condition check within the list with local variables outside of it
- Linq to Entity Groupby and concatinate column
- Nested dictionary nullreferenceexception using linq and entity framework
- LinqKit PredicateBuilder with EF 4 CPT 5 table relationships?
- How to make select statement Dynamic ? Linq
- Select entire duplicate row from datatable with linq c#
- How to use linq to compare only the same day, but in my tale filed only have time type field
- Dynamic + linq compilation error