views:

56

answers:

1

I have an entity name "Forum" with number of properties.
I also created a partial class "Forum" that encapsulates Extra properties like int PostCount.

List<Forum> lForum = null;
lForum= (from forum in Forums
               join post in Posts on forum equals post.Forum into postsInForum
               select new 
               {
                   Forum = forum,    //Fill all 
                   PostCount = postsInForum.Count(post => post.ShowIt == 1) //Fill the "extra" property
                }).ToList();

How can I do it?

Update
If I use a new Class for presentation "ForumAndCount" what will have the same properties as the Forum and an extra property "PostCount" , is it possible to project all Forum's fields on it at once or I must set all of them one by one:

select new ForumAndCount
{
    ForumID= forum.ForumID,
    ForumTitle = forum.Title,
    ForumImg = forum.Img,
    Forum...
    .
    PostCount = postsInForum.Count(post => post.ShowIt == 1) 
}

I can ofcourse Create Forum type property in my ForumAndCount, but i don't want that. })

A: 

Don't put your non-persisted properties in a partial class. That mixes presentation and persistence concerns. Instead, create a separate view/presentation model. Then you can project onto that. Here's an example.

Craig Stuntz
So I should create a special class for presentation layer?
shivesh
I do. Presentation is a separate concern, and the shape of a presentation is often different than the shape of the entity model. Your question is a good example of why.
Craig Stuntz
this is good enough and i will use this in my project, but I'm still interested, is it possible to do what i wanted?
shivesh
And also: the ActionResult class is MVC specific so I would have to refill my class "manually"
shivesh
@Craig Stuntz ,Please see my update
shivesh