Entity with one-to-many collection and LINQ for that collection

33 views
Skip to first unread message

s-stude

unread,
Apr 30, 2012, 3:30:43 PM4/30/12
to nhu...@googlegroups.com
Hi all.

I have next entities:

class Topic
{
public virtual int Id {get; private set;} 
public virtual ICollection<Vote> Votes {get; private set; }
}

class Vote
{
public virtual Topic Topic {get; private set;}
public virtual VoteType VotedTo {get; private set;} // enum VotedUp, VotedDown
}

I need to load from the db the next info - all topics (IDs, actually Names, but in this demo case it does not matter) and two more fields CountOfVotedUp, CountOfVotedDown (aggregates). So as I understand in SQL world we need joins, group by, case and count.

Is it posible to get this info  with LINQ with less operations with db? I mean N+1, additional selects, connections etc.

All that I tried - is to use NH's LINQ, but it's Query<Topic> aggregates only on Topic.Id and I could not count any of Votes collection.

If I was able to explain what I want please write your version of this query, in other case ask me more details.

mynkow

unread,
May 13, 2012, 10:13:03 AM5/13/12
to nhu...@googlegroups.com
session.Query<Topic>().FetchMany(x=>x.Votes)

Is that what you are looking for?
Reply all
Reply to author
Forward
0 new messages