I have a problem in my select statement in my query. This sql query works in SqlServer Management Studio and selects the requested data but in my class its gives me an error "The query syntax is not valid. Near term '*', line 9, column 47." then another error in my From Forums F clause thats after taking out LastPostBy, LastPostDate, LastPostTitle i also get an error in LastPostTitle but like I said it works in SQLServer Management Studio. I initally tried to do a "Exec sproc_Forums_GetForums" stored procedure and that didn't work this is my first attempt using this approach in with Entities. All my query is suppose to do is get a forum and display information from two tables named Forums and Posts
public List<Forum> GetForums()
{
List<Forum> forums = null;
using (EntityConnection conn = new EntityConnection("name=CMSEntities"))
{
conn.Open();
string query = @"
SELECT ForumGroup = (
CASE WHEN ParentID IS NOT NULL THEN
(SELECT Title FROM Forums WHERE ForumID = F.ParentID)
ELSE
(SELECT Title FROM Forums WHERE ParentID IS NULL)
END),
F.Title, F.Description,
ThreadCount = (SELECT COUNT(*) FROM Posts P WHERE P.ForumID = F.ForumID),
LastPostBy = (SELECT TOP 1 AddedBy FROM Posts P WHERE P.ForumID = F.ForumID ORDER BY P.PostID DESC),
LastPostDate = (SELECT TOP 1 AddedDate FROM Posts P WHERE P.ForumID = F.ForumID ORDER BY P.PostID DESC),
LastPostTitle = (SELECT TOP 1 Title FROM Posts P WHERE P.ForumID = F.ForumID ORDER BY P.PostID DESC)
FROM Forums F WHERE ParentID IS NOT NULL
ORDER BY Title
";
EntityCommand cmd = new EntityCommand(query, conn);
using (EntityDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
{
while (reader.Read())
{
Forum forum = new Forum(
(int)reader["ForumID"],
"",
DateTime.Now,
reader["Title"].ToString(),
reader["Description"].ToString(),
0,
false,
null,
null,
null,
true,
reader["ForumGroup"].ToString(),
(int)reader["ThreadCount"],
reader["LastPostBy"].ToString(),
(DateTime)reader["LastPostDate"],
reader["LastPostTitle"].ToString());
forums.Add(forum);
}
return forums;
}
}
}
}