Cannot implicitly convert type 'System.DateTimeOffset' to 'int'

682 views
Skip to first unread message

EisenbergEffect

unread,
Oct 4, 2011, 1:12:29 PM10/4/11
to ravendb
I have a multi-map index which I apparently don't have defined quite
right. It always returns no results. When I view the index in the
Management Studio, I see two errors with the same message: Cannot
implicitly convert type 'System.DateTimeOffset' to 'int' Below is my
index creation task:

public class CharacterOpinion_Concensus :
AbstractMultiMapIndexCreationTask<CharacterConcensus> {
public CharacterOpinion_Concensus() {
AddMap<CharacterOpinion>(
opinions => from opinion in opinions
select new {
opinion.CharacterId,
OwnerId = (string)null,
Visibility = (string)null,
CreatedAt = DateTimeOffset.MinValue,
UpdatedAt = DateTimeOffset.MinValue,
NumberOfFavorites = opinion.IsFavorite ?
1 : 0,
NumberOfRatings = opinion.Rating != 0 ?
1 : 0,
AverageRating = (float)opinion.Rating
});

AddMap<Character>(
characters => from character in characters
select new {
CharacterId = character.Id,
OwnerId = character.Owner.Id,
character.Visibility,
character.CreatedAt,
character.UpdatedAt,
NumberOfFavorites = 0,
NumberOfRatings = 0,
AverageRating = (float)0
});

Reduce = results => from result in results
group result by result.CharacterId
into g
select new {
CharacterId = g.Key,
OwnerId = g.FirstOrDefault(x =>
x.OwnerId != null),
Visibility = g.FirstOrDefault(x =>
x.Visibility != null),
CreatedAt = g.Max(x => x.CreatedAt),
UpdatedAt = g.Max(x => x.UpdatedAt),
NumberOfFavorites = g.Sum(x =>
x.NumberOfFavorites),
NumberOfRatings = g.Sum(x =>
x.NumberOfRatings),
AverageRating = g.Where(x =>
x.AverageRating != 0).Average(x => x.AverageRating)
};
}
}

I'm unsure how to fix this. I just guessed at using
DateTimeOffset.MinValue, but that's probably not the right approach.
Similarly, I guess at using a String in place of Visibililty (which is
an enum). That *seams* to work. I've got some other things I can try,
but wanted to see if anyone had a proven solution.

EisenbergEffect

unread,
Oct 4, 2011, 2:06:12 PM10/4/11
to ravendb
I managed to get rid of the conversion error, but now I have the index
erroring with "Sequence contains no elements"
I updated my index as follows:

public class CharacterOpinion_Concensus :
AbstractMultiMapIndexCreationTask<CharacterConcensus> {
public CharacterOpinion_Concensus() {
AddMap<CharacterOpinion>(
opinions => from opinion in opinions
select new {
opinion.CharacterId,
OwnerId = (string)null,
Visibility = 0,
OwnerId = g.Select(x =>
x.OwnerId).Where(x => x != null).FirstOrDefault(),
Visibility = g.Select(x =>
x.Visibility).Where(x => x != null).FirstOrDefault(),
CreatedAt = g.Max(x =>
(DateTimeOffset)x.CreatedAt),
UpdatedAt = g.Max(x =>
(DateTimeOffset)x.UpdatedAt),
NumberOfFavorites = g.Sum(x =>
x.NumberOfFavorites),
NumberOfRatings = g.Sum(x =>
x.NumberOfRatings),
AverageRating = g.Where(x =>
x.AverageRating != 0).Average(x => x.AverageRating)
};
}
}

My gut tells me there's a problem with the DateTimeOffsets of the
Visibility (which is an enum and I have no idea how to handle in this
case).

Ayende Rahien

unread,
Oct 4, 2011, 2:23:48 PM10/4/11
to rav...@googlegroups.com
The sequence error is probably because of the average, you have to be prepared for a set of results where the AverageRating is all zero

EisenbergEffect

unread,
Oct 4, 2011, 3:57:50 PM10/4/11
to ravendb
That was the main issue. I also discovered that queries against
Visibility weren't working, but fixed that by changing it a string.
Now, I can't seam to get the query statistics to return the proper
TotalResults count. Here's my current index:
OwnerId = g.Select(x =>
x.OwnerId).Where(x => x != null).FirstOrDefault(),
Visibility = g.Select(x =>
x.Visibility).Where(x => x != null).FirstOrDefault(),
CreatedAt = g.Max(x =>
(DateTimeOffset)x.CreatedAt),
UpdatedAt = g.Max(x =>
(DateTimeOffset)x.UpdatedAt),
NumberOfFavorites = g.Sum(x =>
x.NumberOfFavorites),
NumberOfRatings = g.Sum(x =>
x.NumberOfRatings),
AverageRating = g.Sum(x =>
x.AverageRating) / (g.Sum(x => x.NumberOfRatings) > 0 ? g.Sum(x =>
x.NumberOfRatings) : 1)
};
}
}

And here's how it's being used:

public Lazy<Page<CharacterOverview>> ForUser(IUser user, int? limit,
int? offset) {
var query = session.Query<CharacterConcensus,
CharacterOpinion_Concensus>()
.Include<CharacterConcensus>(x => x.CharacterId)
.OrderByDescending(x => x.UpdatedAt)
.Where(x => x.OwnerId == user.Id)
.ToLazyPage(limit, offset);

return new Lazy<Page<CharacterOverview>>(() => {
var page = query.Value;

return new Page<CharacterOverview> {
Asc = page.Asc,
Limit = page.Limit,
Offset = page.Offset,
OrderBy = page.OrderBy,
TotalResults = page.TotalResults,
Items = page.Items.Select(x => new CharacterOverview {
Character = session.Load<Character>(x.CharacterId),
Concensus = x
}).ToList()
};
});
}

ToLazyPage is an extension method that looks like this:

public static Lazy<Page<T>> ToLazyPage<T>(this IRavenQueryable<T>
query, int? limit = 10, int? offset = 0, string orderby = null, bool?
asc = false) {
var paginationInfo = new PaginationInformation(limit, offset);

RavenQueryStatistics stats;
var pagedQuery = query
.Statistics(out stats)
.Skip(paginationInfo.Offset)
.Take(paginationInfo.Limit)
.Lazily();

return new Lazy<Page<T>>(() => {
return new Page<T> {
Items = pagedQuery.Value.ToList(),
Limit = paginationInfo.Limit,
Offset = paginationInfo.Offset,
TotalResults = stats.TotalResults,
OrderBy = orderby,
Asc = asc.GetValueOrDefault()
};
});
}

This is batched with some other queries. It's all working except the
statistics.

On Oct 4, 2:23 pm, Ayende Rahien <aye...@ayende.com> wrote:
> The sequence error is probably because of the average, you have to be
> prepared for a set of results where the AverageRating is all zero
>

Ayende Rahien

unread,
Oct 5, 2011, 2:58:16 AM10/5/11
to rav...@googlegroups.com
inline

On Tue, Oct 4, 2011 at 9:57 PM, EisenbergEffect <rob.ei...@gmail.com> wrote:
That was the main issue. I also discovered that queries against
Visibility weren't working, but fixed that by changing it a string.

What  was it before that?
 
Now, I can't seam to get the query statistics to return the proper
TotalResults count. Here's my current index:


Answer in the second thread. 
Reply all
Reply to author
Forward
0 new messages