I have an entity that represents a Tweet from Twitter like so:
public class Tweet
{
public virtual long Id { get; set; }
public virtual string Username { get; set; }
public virtual string Message { get; set; }
// other properties (snip)...
public virtual ISet<long> VoterIds { get; protected set; }
}
I'm trying to run a query in NHibernate that selects a list of tweets with an additional column that denotes whether a particular user by their UserId has voted for each Tweet. When I user votes for a Tweet, it's stored in the 'VoterIds' collection above.
I'm using a collection of value Types for this, since I'm only really interested in the Twitter UserId to determine if a user has already voted for a particular tweet. Hence why it's an ISet<long>
instead of ISet<Vote>
I'm trying to use projections like so:
long userId = 123;
IList<TweetReport> tweets = Session.CreateCriteria<Tweet>()
.SetProjection(Projections.ProjectionList()
.Add(Projections.Id(), "Id")
.Add(Projections.Property("Username"), "Username")
.Add(Projections.Property("Message"), "Message")
.Add(Projections.Conditional( //---- WHAT GOES HERE!!??
.SetResultTransformer(Transformers.AliasToBean<TweetReport>())
.List<TweetReport>();
I thought the correct method was to use Projections.Conditional, but I'm not sure how to use it. Can someone help me fill in the //---- WHAT GOES HERE!!??
bit in the above code.
I tried using Expressions.In:
.Add(Projections.Conditional(Expressions.In("VoterIds", new object[] { userId }),
Projections.Constant(true), Projections.Constant(false)))
...but it gave me a 'Cannot use collections with InExpression' error. Please help!
Update: I'm beginning to think that it isn't possible to query collections of value types at all, and that I should be using a full-blown entity like so:
public virtual ISet<Vote> Votes { get; protected set; }
...would this be the case?