Getting an empty LINQ error; can't then use?

I am trying to run the following in LINQ

double totalDistance = (from g in db.Logs join 
    h in db.Races on g.raceId equals h.RaceId 
    where g.userId == id select h.distance).Sum();

However, get the error:

Casting the value to the Double value type failed because the materialized value is zero. Both the general result parameter and the query should use a type with a null value.

I tried to add ?? 0; such that:

double totalDistance = (from g in db.Logs join 
    h in db.Races on g.raceId equals h.RaceId 
    where g.userId == id select h.distance).Sum() ?? 0;

As indicated in other posts, this results in an error:

Operator

'??' cannot be applied to double or int operands

Any suggestions?

EDIT: my model

namespace RacePace.Models
{
public class Race
{
    public int RaceId { get; set; }

    [DisplayName("Race Setting")]
    public string place { get; set; }
    [DisplayName("Distance (km)")]
    public double distance { get; set; }
    [DisplayName("Date")]
    public DateTime date { get; set; }
    [DisplayName("Commencement Time")]
    public DateTime timeStarted { get; set; }
    [DisplayName("Active")]
    public Boolean active { get; set; }
    [DisplayName("Description")]
    public string description { get; set; }
    [DisplayName("Creator")]
    public int UserId { get; set; }
}
}
+5
source share
4 answers

Do you have to double your double in your model to do ??? Job. From http://msdn.microsoft.com/en-us/library/ms173224.aspx :

,

public double? distance { get; set; }

???

+2

, .

double totalDistance = (double)((from g in db.Logs join h in db.Races on g.raceId equals h.RaceId where g.userId == id select h.distance).Sum() ?? 0);

: double.Parse

var someObject = (from g in db.Logs join h in db.Races on g.raceId equals h.RaceId where g.userId == id select h.distance);
double totalDistance = (someObject !=null)? someObject.Sum() : 0;
+1

, - , , . .

Linq

double totalDistance = 
   (from g in db.Logs join h in db.Races 
    on g.raceId equals h.RaceId where g.userId == id 
    select h).Sum(x => x.distance) ?? 0;

a >

NULL, , , - , , . EDIT:

double? totalDistanceTemp = 
       (from g in db.Logs join h in db.Races 
        on g.raceId equals h.RaceId where g.userId == id 
        select h).Sum(x => x.distance);
double totalDistance = totalDistanceTemp ?? 0;

THAT , nullables. , , , / LINQ, , - . , .

double? totalDistanceTemp = 
       (from g in db.Logs join h in db.Races 
        on g.raceId equals h.RaceId where g.userId == id 
        select h).Sum(x => (Double?) x.distance);
double totalDistance = totalDistanceTemp ?? 0;

double? totalDistanceTemp = 
       (from g in db.Logs join h in db.Races 
        on g.raceId equals h.RaceId where g.userId == id 
        select ((Double?)h.distance )).Sum();
double totalDistance = totalDistanceTemp ?? 0;

, , : . , LINQ to Objects (nullable ). . / , , , LINQ .

double totalDistance = 
       (from g in db.Logs join h in db.Races 
        on g.raceId equals h.RaceId where g.userId == id 
        select h.distance).AsEnumerable().Sum();

, - , , , - ( , ).

0

, . , null, , . MSDN. , , totalDistance. MSDN.

-1

All Articles