Linq Errors on ToList(), Any(), Count(), Sum() - c#

I am getting the following errors, they are all related to system.linq. The strange thing is that the method seems to work sometimes, the errors are not happening consistently.
Count() Code:
using (var dbContext = new Entities(GlobalStuff.EntityConnection))
{
heartBeat.NumSlipsInSecurityUploadQueue = dbContext.SecurityUploadQueues.Count();
heartBeat.NumSlipsInDataStreamQueue = dbContext.DataStreamQueues.Count();
}
SecurityUploadQueues Property:
Public ReadOnly Property SecurityUploadQueues() As ObjectSet(Of SecurityUploadQueue)
Get
If (_SecurityUploadQueues Is Nothing) Then
_SecurityUploadQueues = MyBase.CreateObjectSet(Of SecurityUploadQueue)("SecurityUploadQueues")
End If
Return _SecurityUploadQueues
End Get
End Property
Private _SecurityUploadQueues As ObjectSet(Of SecurityUploadQueue)
The SecurityUploadQueue property is an EntityObject
Count() Error:
Error Uploading HeartBeat System.InvalidOperationException: Sequence contains more than one element
at System.Linq.Enumerable.Single[TSource](IEnumerable1 source)
at System.Data.Objects.ELinq.ObjectQueryProvider.<>c__111.b__11_3(IEnumerable1 sequence)
at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable1 query, Expression queryRoot)
at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
at SettlerService.SettlerService.UploadHeartBeat()
Any() Code:
var dataToUpload = (from bet in dbContext.DataStreamQueues select bet).Take(200);
if (dataToUpload.Any())
Any() Error:
System.InvalidOperationException: The specified cast from a
materialized 'System.Int32' type to the 'System.Boolean' type is not
valid. at
System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader1.GetValue(DbDataReader
reader, Int32 ordinal) at lambda_method(Closure , Shaper ) at
System.Data.Common.Internal.Materialization.Coordinator1.ReadNextElement(Shaper
shaper) at
System.Data.Common.Internal.Materialization.Shaper1.SimpleEnumerator.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable1 source) at
System.Data.Objects.ELinq.ObjectQueryProvider.<>c__111.<GetElementFunction>b__11_3(IEnumerable1
sequence) at
System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable1
query, Expression queryRoot)
at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.Any[TSource](IQueryable1 source)
at SettlerService.SettlerService.UploadDataStream()
I've done some googling and can't find much on this issue. I thought it might be related to the version of the system.core library (which system.linq is part of) but couldn't see an issue. I am using .Net 4.7.2

The second error implies that there is one columns in your DataStreamQueues database table with type of Int32 but the type of the equivalent property in DataStreamQueues entity is bool; so check if all columns and properties types match.
Regarding the first exception, it's a bit strange! because this exception suggests that somewhere in your code you are calling Single() method on a set that has multiple items and as Single() expects only one item, it throws an exception, however I don't see any calls to Single() in the provided code.

Related

DbSet.Where in EF fetching all the data

I have using the below code to fetch the matching data based on the where parameter and then removing them but not sure why in the log the query which gets executed shows that it is first fetching all the records from table irrespective of the where filter.
void function(Func<TEntity, bool> where)
{
IQueryable objects = DbSet.Where<TEntity>(where).AsQueryable();
foreach(var obj in objects)
DbSet.remove(obj) ;
}
Entity framework uses expression trees to be able to convert lambda expression to sql. You are taking a Func<TEntity, bool>, this causes the DbSet to be converted to a IEnumerable.Where, rather than using a IQueryable.Where, causing all items to be fetched.
You should probably replace the parameter with an Expression<Func<TSource, bool>>. Note that not all expressions can be converted to SQL, and this may result in runtime errors, so you need to be a bit careful how you write expressions.

JSON Type not supported for int datatype --> JSON type not supported. (Parameter 'ValueKind')\r\nActual value was Number

I have a class Person that has an Age property which is an int. It is persisted down to Cosmos Graph DB. I use the GremlinClient to fetch the record and then I have a Materialize method to rebuild <T>. If the Person class doesn't have any int types it works fine, however my method fails somewhere.
Coming back from the Graph, my result looks good:
Few things to note about the above:
it is an IDictionary<string, object> and it has 4 entry.
the 4th entry actually holds the properties of the Person object as they exist in the vertex in the graph
Few things to note:
Although the resulting value in the dictionary entry (object) is a value "Bob", "Oak
, 32, etc. it comes from the Gremlin client as System.Linq.Enumerable.SelectEnumerableIterator<System.Text.Json.JsonElement, object> which my method attempts to extract the value and then recreate <T> and return T.
Here's how I found it.link I have made some changes to this method but it still throws the same error, apparently its the SelectEnumerableIterator doesn't like Ints for some reason.
Hoping one of you can help me!
Error and Stack Trace:
ActualValue: Number
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233086
HelpLink: null
InnerException: null
Message: "JSON type not supported. (Parameter 'ValueKind')\r\nActual value was Number."
ParamName: "ValueKind"
Source: "Gremlin.Net"
StackTrace: " at Gremlin.Net.Structure.IO.GraphSON.GraphSONReader.ToObject(JsonElement graphSon)
\r\n at Gremlin.Net.Structure.IO.GraphSON.GraphSONReader.<ReadDictionary>b__6_1(JsonProperty property)
\r\n at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
\r\n at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
\r\n at Gremlin.Net.Structure.IO.GraphSON.GraphSONReader.ReadDictionary(JsonElement jsonDict)
\r\n at Gremlin.Net.Structure.IO.GraphSON.GraphSONReader.ToObject(JsonElement graphSon)
\r\n at Gremlin.Net.Structure.IO.GraphSON.GraphSONReader.<ToObject>b__4_0(JsonElement t)
\r\n at System.Linq.Enumerable.SelectEnumerableIterator`2.ToList()" TargetSite: {System.Object ToObject(System.Text.Json.JsonElement)}
There's a discussion on the same issue here, and I've posted a work-around until Cosmos updates their support.

NullReferenceException in EF Core when using StringComparison overload for predicates

We are converting a project from using EF to using EF Core. We have the following line of code which used to work but now does not:
// throws NullReferenceException
var u = db.Users.FirstOrDefault(x => x.PresenterID.Equals(uid, StringComparison.OrdinalIgnoreCase));
However, if we don't use the StringComparison overload, it works:
// this works
var u = db.Users.FirstOrDefault(x => x.PresenterID.Equals(uid));
This is a large project and we would like to avoid finding and modifying all code that does such comparisons. Why does this throw a NullReferencException and can it be avoided without changing our code? Here is the stack trace. Thanks.
at lambda_method(Closure , User ) at
System.Linq.Enumerable.WhereEnumerableIterator1.MoveNext() at
System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source)
at
Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ResultEnumerable1.GetEnumerator()
at
Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__172.MoveNext()
at
Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext()
at System.Linq.Enumerable.First[TSource](IEnumerable1 source) at
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_1`1.b__0(QueryContext
qc) at
Our.Project.Service.ContractsFolderService.ValidateContractUsers(BIContract
xmlContract, IDataContext db) in
C:\Projects\Out.Project\Main\Service\ContractsFolderService.cs:line
436
According to this open issue on the EntityFrameworkCore Github repo, that overload is not yet supported with LINQ to SQL. See this specific comment which gives some detail on the problem preventing this overload from being translated: https://github.com/aspnet/EntityFrameworkCore/issues/1222#issuecomment-443116169
I'm guessing that the null reference exception is occurring because its pulling back all the results to evaluate your string comparison on the client side, and for one or more of those results PresenterID is null.

Creating Linq to EF types for GroupBy Key selector

Using Net 4.5.1
Ran into a problem with the construction of Queryable that mixed in Enumerable lambda compilations that was pulling everything from the database into memory. Thanks #Servy for the help there.
I was using a dictionary as the TKey in a GroupBy selector I created with expressions. A dictionary apparently can't be used in this fashion (see gist line 37 GetGroupByDictionary method). Tried with a simple object array and ran into type conversion problems (i.e. EF expects primitive or Enum). Tested a hard-code array of Int but the TKey selector type for the GroupBy isn't handled by the provider (i.e. database). The GroupBy in the Queryable looked like:
GroupBy(Param_0 =>
new [] {
Convert(Param_0.Respondent.currentVisitYear),
Convert(Param_0.Respondent.currentVisitMonth)
})
My intent was to use either a dictionary or array to house an unknown number of group-by key selector (i.e. the client decides what to group by which can be one property or 10).
Can these types for a key selector can ever be valid/used when going from Linq to EF?
If I new in advance the number of group-by then an anonymous type would do, like:
.GroupBy(x =>
new {
Year = x.Respondent.currentVisitYear,
Month = x.Respondent.currentVisitMonth
}
)
It is possible to create types at runtime (thanks this example). Is this my only alternative?
Update
Made a gist with the help of Ethan J. Brown's code to create an type on the fly as the TSource result of the GroupBy clause. At line 9 in Calling Method I create the IGrouping returning the type and Expression in a Tuple. At line 24 I create the Select clause's selector expression.
Everything compiles but bombs out when with:
{"The LINQ expression node type 'Lambda' is not supported in LINQ to Entities."}
If I rip out the dynamic type (i.e. hard-code an inline lambda in the GroupBy) with a normal type:
GroupBy(x=> new CommonGroupBy(
Year : (int) x.Respondent.currentVisitYear,
Month : (int) x.Respondent.currentVisitMonth
)
and adjust the selector code to return (i.e. CommonGroupBy rather than object):
Expression<Func<IGrouping<CommonGroupBy, ChartJoin>, AveragePartySize>>
then the query runs. Something in the hand off of dynamic types isn't right.
Update 2
The error above was due to line 32 in the Group by stuff file. Right now battling with the next error:
"Unable to cast the type 'System.Object' to type 'Year;Nullable`1;Month;Nullable`1;'. LINQ to Entities only supports casting EDM primitive or enumeration types."

Passing a compiled Expression to Linq To NHibernate

I have a collection of compiled expressions which I combined into one expression because I want to build dynamically my linq query. See ExpressionTree Compile() method generate a stackoverflow exception for the reason I used compiled expression in order to prevent from stackoverflow because in my computer when the query contains more than 7000 expressions, it throws this exception.)
Then I use the new generated expression and pass it to my FindAll method. Problem, NHibernate is not able to execute the query and says:
unable to cast object of type 'nhibernate.hql.ast.parameter' to type 'nhibernate.hql.ast.hqlbooleanexpression'
public IList<T> FindAll(Expression<Func<T, bool>> criteria)
{
return SessionFactory.GetCurrentSession()
.QueryOver<T>()
.Where(criteria)
.List();
}
I debuged and found that nhibernate tries to convert the compiled expression to boolean expression in file HqlTreeNode (method: HqlTreeNodeExtensions.AsBooleanExpression(this HqlTreeNode node) which of course doesn't works. Then what solution should I use ?
Here what the criteria variable looks like:
(Invoke(value(System.Func`2[Something.SomeEntity,System.Boolean]) // this don't work
For information, if it wasn't compiled, it would have looked like something like this:
([someEntity].UserID == 1) // this works
Thank you.

Categories