質問

私はこのフォーラムをコード化していると私は、LINQに新たなんだので、ユーザーはメインページに当たったとき、私はこの問題に遭遇しました。私はこのようなフォーラムの一覧を表示するテーブルが欲しいます:

Forum  --- Topics (count) --- Posts (count) --- LastPostUserId --- LastPostTime

私は、次のSQLのテーブルを持っています

Forums:
ForumId (int32),
Title (string),
Description (string)

ForumThreads:
ThreadId (int32),
ForumId (int32),
UserId (guid),
Subject (string),
Views (int32),
CreateDate (DateTime)

ForumPosts:
PostId (int32),
ThreadId (int32),
UserId (guid),
Post (string),
CreateDate (datetime)

ありがとう...

役に立ちましたか?

解決

あなたがメンバーシップを使用して、あなたのDBMLでaspnet_Usersを含めたくない場合は、ユーザーの名前を表示します。

...
LastPostUserId = posts.OrderByDescending(p=>p.PostId).Take(1).Select(p=> Membership.GetUser(p.UserId))
...

もう一つの変更は、あなたの投稿のサンプルを作るために、より良いビットは、変数の記事でorderbydescendingを追加することです。 その後、select句から4回繰り返しOrderByDescendingをドロップすることができます:

from forum in Forums
let posts = ForumPosts.Where(p => p.ForumThreads.ForumId.Equals(forum.ForumId)).OrderByDescending(p=>p.PostId)
select new
{
    Forum = forum.Title,
    Description = forum.Description,
    Topics = forum.ForumThreads.Count(),
    Posts = posts.Count(),
    LastPostId = posts.Take(1).Select(p=>p.PostId),
    LastPostThreadId = posts.Take(1).Select(p=>p.ThreadId),
    LastPostUserId = posts.Take(1).Select(p=>p.UserId),
    LastPostTime = posts.Take(1).Select(p=>p.CreateDate)
}

あるいはクリーナーます:

from forum in Forums
let posts = ForumPosts.Where(p => p.ForumThreads.ForumId.Equals(forum.ForumId))
let lastPost = posts.OrderByDescending(p=>p.PostId).Take(1)
select new
{
    Forum = forum.Title,
    Description = forum.Description,
    Topics = forum.ForumThreads.Count(),
    Posts = posts.Count(),
    LastPostId = lastPost.PostId,
    LastPostThreadId = lastPost.ThreadId,
    LastPostUserId = lastPost.UserId,
    LastPostUserName = Membership.GetUser(lastPost.UserId),
    LastPostTime = lastPost.CreateDate
}
試験番号最後の投稿はありません。このコードはカントー

、私はテイク(1)はヌル..

であれば、それはエラーをスローかもしれないと思います

他のヒント

from forum in forums
from posts in db.ForumPosts.Where(p => p.Thread.ForumId.Equals(forum.ForumId))
select new
{
Forum = forum.Title, 
Topics = forum.ForumThreads.Count(),
Posts = posts.Count(),
LastPostBy = posts.OrderByDescending(p => p.CreateDate).FirstOrDefault(p => p.UserId),
LastPostTime= posts.Max(p => p.CreateDate))
}
ofcourseの

テストされていないが、ここからスタートし、SQLクエリ(複数可)をチェックし、それが実行され、それが最適化を必要とするなら、私が知っているようにしようとします。

このほとんどないトリック(それはいくつかの恐ろしいSQLを生成しますが、; - )...)

from forum in Forums
let posts = ForumPosts.Where(p => p.ForumThreads.ForumId.Equals(forum.ForumId))
select new
{
    Forum = forum.Title,
    Description = forum.Description,
    Topics = forum.ForumThreads.Count(),
    Posts = posts.Count(),
    LastPostId = posts.OrderByDescending(p=>p.PostId).Take(1).Select(p=>p.PostId),
    LastPostThreadId = posts.OrderByDescending(p=>p.PostId).Take(1).Select(p=>p.ThreadId),
    LastPostUserId = posts.OrderByDescending(p=>p.PostId).Take(1).Select(p=>p.UserId),
    LastPostTime = posts.OrderByDescending(p=>p.PostId).Take(1).Select(p=>p.CreateDate)
}

最後のもの - 私は「Aspnet_Users」にSQLテーブル「ForumPosts」から関係を持っていると私は列LastPostUserNameとしてAspnet_Users.UserNameを表示したいと思います...どのように行うことができますか?そして、どのようにあなたが全体のクエリを最適化するのでしょうか?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top