tags:

views:

44

answers:

1

Hi - I have a object:

public class ForumTopic
{
        public Guid ForumTopicId { get; set; }
        public Guid OwnerId { get; set; }
        public Guid CategoryId { get; set; }
        public DateTime CreatedDate { get; set; }
        public string Topic { get; set; }
        public bool IsSticky { get; set; }
        public bool IsClosed { get; set; }
        public int ViewCount { get; set; }
        public int TotalComments { get; set; }
        public Comment LastComment { get; set; }
}


I then have a Linq query and I need to figure out how to populate the LastComment and I can't create a new ForumTopic becuase Linq tells me that is breaking the rules...

IQueryable<ForumTopic> query = from topic in context.ForumTopics

                               join comment in context.Comments on topic.ForumTopicId equals comment.TargetId into topicComments
                               from lastComment in topicComments.DefaultIfEmpty().OrderByDescending(c => c.CreatedDate).Take(1)

                               orderby topic.IsSticky, topic.CreatedDate descending
                               select topic;

The query returns everything correct in SQL, however topic.LastComment is null.

Any ideas?

+1  A: 

The main problem is you're not assigning the LastComment. Without a relationship established in the database, it has no idea how to fill that object.

You're going to need to manually assign the comment:

IQueryable<ForumTopic> query = from topic in context.ForumTopics

orderby topic.IsSticky, topic.CreatedDate descending
select new ForumTopic 
{
  ForumTopicId = topic.ForumTopicId,
  OwnerId = topic.OwnerId,
  // .. etc
  LastComment = topic.Comments.OrderByDescending(c => c.CreatedDate).FirstOrDefault();
};

Obviously, I'm assuming you have a parent-child relationship between topic and comments. If you don't, you should reconsider how you're using linq :p

JustLoren
As JustLoren said, your Linq query is incorrect.I think the JustLoren's query is what you might need.
MNZ
I have:join comment in context.Comments on topic.ForumTopicId equals comment.TargetId into topicCommentsin there, that is the relationship. And I tried the new ForumTopic {} thing but Linq told me it was forbidden, probably becuase ForumTopic is a type Linq to SLQ has mapped in my mapping.xml.Any other ideas?
@Bryan: I was assuming you are using the L2S designer. I'm not familiar with the mapping.xml implementation. When I meant relationship, I meant in your _schema_ not in your select statement. One of the huge benefits of Linq is the way it manages relationships of objects and allows you to query against them. I'm not sure why your compiler was complaining about it being forbidden - there's numerous examples on the web where the L2S type is instantiated and filled explicitly as part of the select command. Does your mapping.xml have a mapping defined for LastComment? If so, what is it?
JustLoren
<Table Name="ForumTopics"> <Type Name="Objects.ForumTopic"> <Column Name="ForumTopicId" Member="ForumTopicId" IsPrimaryKey="true" CanBeNull="false" /> <Column Name="OwnerId" Member="OwnerId" CanBeNull="false" /> <Column Name="CategoryId" Member="CategoryId" CanBeNull="true" /> <Column Name="CreatedDate" Member="CreatedDate" CanBeNull="false" /> <Column Name="Topic" Member="Topic" CanBeNull="false" /> <Column Name="IsSticky" Member="IsSticky" CanBeNull="false" /> <Column Name="IsClosed" Member="IsClosed" CanBeNull="false" />
<Column Name="ViewCount" Member="ViewCount" CanBeNull="false" /> <Column Name="TotalComments" Member="TotalComments" IsDbGenerated="true" /> <Association Member="LastComment" OtherKey="TargetId" ThisKey="ForumTopicId" /> </Type> </Table><Table Name="Comments"> <Type Name="Objects.Comment"> <Column Name="CommentId" Member="CommentId" IsPrimaryKey="true" CanBeNull="false" /> <Column Name="TargetId" Member="TargetId" CanBeNull="true" /> <Column Name="CreatedDate" Member="CreatedDate" CanBeNull="false" />
</Type> </Table>This comment box wouldn't let me paste them all at once.
You only have one defined relationship: `<Association Member="LastComment" OtherKey="TargetId" ThisKey="ForumTopicId" />` This is binding *all* comments into the LastComment field. That means your name "LastComment" is very misnamed. What you really want to do is rename your LastComment association to `Comments` and then use my example code.
JustLoren