문제

이 포럼을 코딩하고 있으며 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을 추가하는 것입니다. 그러면 선택 절에서 4 배 반복 된 주문을 삭제할 수 있습니다.

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
}

마지막 게시물이 없을 때이 코드를 테스트하십시오. Take (1)이 null이면 오류가 발생할 수 있다고 생각합니다.

다른 팁

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))
}

논문을 제외하고 여기에서 시작하여 여기에서 시작하여 실행되는 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)
}

마지막으로 - SQL 테이블 'Forumposts'에서 'aspnet_users'로의 관계가 있으며 열 aspnet_users.username 열을 lastpostusername으로 표시하고 싶습니다 ... 어떻게 할 수 있습니까? 그리고 전체 쿼리를 어떻게 최적화 하시겠습니까?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top