简体   繁体   中英

Get SUM of Nullable Column using WHERE clause

I am having some problems retrieving the SUM of a column in EF6 using a WHERE clause.

I am using this:

double oh2 = raptorEntities.OnlineHistories
    .Where(us => us.Id == user.Id)
    .Sum(r => (double?) r.SessionTime) ?? 0; // Need the cast as we may get a null !

The idea is get the SUM of SessionTime column in OnlineHistories WHERE Userid = x

Now the SessionTime column is a computed column and contains values... and does allow NULL

Anyone able to fix this expression so that it works?

UPDATE ONE

With the suggested Fix:

double oh2 = raptorEntities.OnlineHistories.Where(us => us.Id == user.Id &&
 us.SessionTime.HasValue).Sum(r => r.SessionTime);

I have to cast it like so, and receive an error:

double oh2 = raptorEntities.OnlineHistories.Where(us => us.Id == user.Id &&
 us.SessionTime.HasValue).Sum(r => (double)r.SessionTime);

Error Error 2019: Member Mapping specified is not valid. The type 'Edm.Double[Nullable=True,DefaultValue=]' of member 'SessionTime' in type 'RaptorModel.OnlineHistory' is not compatible with 'SqlServer.int[Nullable=True,DefaultValue=,StoreGeneratedPattern=Computed]' of member 'SessionTime' in type 'RaptorModel.Store.OnlineHistory'. RaptorDb C:\\Users\\Dave\\documents\\visual studio 2015\\Projects\\RaptorService\\RaptorDb\\RaptorModels.edmx 835

With the Mapping Looking like this:

<EntitySetMapping Name="OnlineHistories">
            <EntityTypeMapping TypeName="RaptorModel.OnlineHistory">
              <MappingFragment StoreEntitySet="OnlineHistory">
                <ScalarProperty Name="SessionTime" ColumnName="SessionTime" />
                <ScalarProperty Name="Id" ColumnName="Id" />
                <ScalarProperty Name="UserId" ColumnName="UserId" />
                <ScalarProperty Name="OnlineAt" ColumnName="OnlineAt" />
                <ScalarProperty Name="OfflineAt" ColumnName="OfflineAt" />
              </MappingFragment>
            </EntityTypeMapping>

UPDATE TWO: SessionTime does not have a type:

SessionType表SessionTime属性

Just filter those rows out:

double oh2 = raptorEntities.OnlineHistories
.Where(us => us.Id == user.Id && us.SessionTime.HasValue)
.Sum(r => r.SessionTime);

EDIT:

You are looking in edmx:Mappings . Instead look in edmx:StorageModels for type. Also in Sql Server in Object Explorer expand tables->OnlineHistories->Columns and you probably should be able to see that type is int . Somehow in your model it is of type double . I can reproduce this error if I manually change column type from int to double in edmx . But by default it is created as int in my model. You can try to delete that table from model and import it again.

One note Sum function seems to eliminate NULL s itself:

double oh2 = raptorEntities.OnlineHistories
.Where(us => us.Id == user.Id)
.Sum(r => r.SessionTime);

Why not filter out the nulls?

double oh2 = 
raptorEntities
.OnlineHistories
.Where(us => us.Id == user.Id && us.SessionTime != null)
.Sum(r => (double)r.SessionTime);

Since you intend that the nulls are coalesced to zero, ignoring them will have no effect on the Sum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM