Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1091463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:31:53+00:00 2026-05-16T23:31:53+00:00

OK, first my simple Domain Model is 2 classes with a one-to-many relationship, a

  • 0

OK, first my simple Domain Model is 2 classes with a one-to-many relationship, a simple Parent -> child relationship. A ‘Tweet’ has one or more ‘Votes’, but each Vote belongs to just one Tweets etc.

public class Tweet
{
    public virtual long Id { get; set; }
    public virtual string Username { get; set; }
    public virtual string Message { get; set; }

    public virtual ISet<Vote> Votes { get; set; }
}

public class Vote
{
    public virtual long Id { get; set; }
    public virtual long TwitterUserId { get; set; }
    public virtual DateTime VotedDate { get; set; }

    public virtual Tweet Tweet { get; set; }
}

I’m trying to write a query in either HQL, ICriteria or NHibernate LINQ, that selects all Tweets, but also add two columns that selects:

  • A count of the number of votes, and…
  • Whether a particular user has voted for that tweet, based on a particular TwitterUserId

With the two extra columns, I’m not expecting to get Tweet domain object back, and would probably need to run a Report query, that’s OK. But I’m struggling to figure out how to write this query.

I know how to write this as a Stored Procedure, or using LINQ 2 SQL. If it helps I will express this as a LINQ to SQL query.

long userId = 123;

var tweets = from t in dataContext.Tweets
             where t.Application == app
             orderby t.PostedDate desc
             select new TweetReport()
             {
                 Id = t.Id,
                 Username = t.Username,
                 Message = t.Message,
                 TotalVotes = t.Votes.Count(),
                 HasVoted = t.Votes.Any(v => v.TwitterUserId == userId)
             };

I know this will work in LINQ 2 SQL, and generate reasonably efficient T-SQL, but I can’t figure out how to write this in NHibernate.


Update: I tried running the above LINQ query in NHibernate by using the NHibernate LINQ provider built for NHibernate v2, eg:

var tweets = from t in Session.Linq<Tweet>()
             where (snip)

But it didn’t work. Has LINQ support in Nhibernate 3.0 improved? I’m a bit reluctant to use version 3.0 because it’s still alpha, but if this will work, then I might give it a go.


Update 2: Thanks to Diego Mijelshon’s suggestion, I upgraded to NHibernate 3.0 alpha 2 and wrote the query in LINQ:

var tweets = from t in Session.Query<Tweet>()
             where t.App == app
             orderby t.PostedDate descending
             select t;

int totalRecords = tweets.Count();

var pagedTweets = (from t in tweets
                   select new TweetReport()
                   {
                       Id = t.Id,
                       TwitterId = t.TweetId,
                       Username = t.Username,
                       ProfileImageUrl = t.ImageUrl,
                       Message = t.Message,
                       DatePosted = t.PostedDate,
                       DeviceName = t.Device.Name,
                       DeviceUrl = t.Device.Url,
                       TotalVotes = t.Votes.Count(),
                       HasVoted = t.Votes.Any(v => v.TwitterUserId == userId)
                   })
                   .Skip(startIndex)
                   .Take(recordsPerPage)
                   .ToList();

return new PagedList<TweetReport>(pagedTweets,
    recordsPerPage, pageNumber, totalRecords);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:31:53+00:00Added an answer on May 16, 2026 at 11:31 pm

    It’s exactly the same with NHibernate 3; just replace dataContext.Tweets with session.Query<Tweet>.

    Alternatively, you can create a context class that exposes session.Query<Tweet> as an IQueryable<Tweet> Tweets property, then the code would be 100% unchanged.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a simple code-first MVC 3 blog like application. My model has three
My domain model with associations is as follows: Project has many Task s Task
http://dspace.dial.pipex.com/town/green/gfd34/art/bloopers.html The first one seems simple; return strcpy(malloc(strlen(s)), s); malloc could return null ,
First, a simple example to describe my problem. Model public class User { public
The convention is that you use your company's domain. That is quite simple but
I've written myself a nice simple little domain model, with an object graph that
I'm trying to set up a relationship as follows. Each Master item has one
We have a simple domain model: Contact, TelephoneNumber and ContactRepository. Contact is entity, it
I'm trying to write my first simple mvc app. I have a Main View
I'm playing with Mockito (1.9.5) and stuck at first simple test case: List mockedList

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.