Entity Framework calling MAX on null on Records -


I get the following exception when calling Max () on the IQueryable and zero records.

Cast value in 'Value Type 32' failed because the physical value is zero. Either the type of result must be used in generic parameters or a tap type in the query.

  var version = ctx.Entries. Where (E => E. competition. Competitive ID == storeCompetition .CompetitionId). Max (E => ED version);  

Now I understand why this is why my question is, what is the best way to do this when the table is empty, code works down and resolves this problem, But its not very ugly MaxOrDefault () concept?

  int? Version = ctx.Entries. Where (E => E. competition. Competitive ID == Store Contest competition). Choose (E = & gt; (Int?) E. version). Max ();  

Yes, casting a cable for T is the recommended way to deal with the problem Asking unit questions from LINQ, a MaxOffer Default () method whose correct signature looks like an interesting idea, but you will just need an additional version for each method that presents this problem, which is very good Will not Scale By Way

In CLR How things work and how it actually works on the database server, it is one of the many mismatches, the signature of the Max () method is defined in this way because the result type is the same as the input type on the CLR There is a possibility. But the results on the database server can be futile. For this reason, you need to input the input (though how you write your query, it may be enough to produce the product) t. To make a blank.

Here is a solution that looks a bit simpler than what you have:

  var version = ctx.Entries Where (E => E. competition. Competitive ID == store competition competition). Max (E => (int?) E.Version);  

Hope it helps.


Comments