2014-11-25 69 views
1

我在DB如何通過

  1. VoucherQuestionCollection
  2. PostComment三個表(包含POST表的多個評論)
  3. 與秩序和組運行的Linq

表格之間的關係是

VoucherQuestionCollection.discussionid = Post.PostId 
and Post.PostId = PostComment.PostId 

VoucherQuestionCollection表有一列名爲「WonDate」的列,其中包含日期時間值。我需要的是,我需要獲取從VoucherQuestionCollection表基於以下條件的數據: -

  1. VoucherQuestionsCollection的WonDate應該是最少的日期(說這即將過期第一個)
  2. PostComment的評論數也應該是最不重要的。 (這是評論最低的帖子)

那麼,如何在Linq中構建查詢以通過考慮上述條件生成所需的輸出?

這是我到目前爲止所做的,但堅持如何進一步發展。

var voucherQuestionTotalCollection = (from voucherQuestionCollection in this.GetDbSet<Jimble.Model.VoucherQuestionCollection>() 
                join posts in this.GetDbSet<Jimble.Model.Post>() on voucherQuestionCollection.DiscussionId equals posts.PostId 
                join comm in this.GetDbSet<Jimble.Model.PostComment>() on posts.PostId equals comm.PostId into gj 
                from sub in gj.GroupBy(c => c.PostId).Select(g => new { Available = g.Count() }).DefaultIfEmpty() 
                where voucherQuestionCollection.UserId != userId && voucherQuestionCollection.VoucherWonDate > DateTime.Now.AddDays(-7) 
                orderby voucherQuestionCollection.VoucherWonDate descending 
                select new { Id = voucherQuestionCollection .Id, 
                   VoucherWonDate = voucherQuestionCollection.VoucherWonDate, 
                   DiscussionId = voucherQuestionCollection.DiscussionId, 
                   TotCommentCount = sub.Available 
                }).OrderBy(c=>c.TotCommentCount); 

樣品表

VoucherQuestionCollection     Post     PostComment 
Id DiscussionId WonDate     Id  PostText   Id PostId CommentText 

1 1    2014-11-21 17:13:00.113 1  FirstPost  1 1  CommentText1 
2 2    2014-11-22 17:13:00.113 2  SecondPost  2 1  CommentText2 
3 3    2014-11-23 17:13:00.113 3  ThridPost  3 2  CommentText3 
4 4    2014-11-24 17:13:00.113 4  FourthPost  4 2  CommentText4 
                    5 2  CommentText5 
                    6 3  CommentText6 

這是輸出,我需要 如果表中的狀態是像上面顯示我的預期輸出是VoucherCollectionTable的第三項,因爲它在評論只有一個計數表。 如果PostComment表具有相同的意見,我需要得到的1來自VoucherCollectionTable進入,因爲至少日期爲2014年11月21日17:13:00.113

注意:本VoucherQuestionCollection將有多個條目,我需要根據上述條件僅取6條記錄。

有鬧明白我上面的解釋,只是這是SQL查詢

Select * from VoucherQuestionCollections a 
inner join Posts b on a.DiscussionId = b.PostId 
Left join (Select PostComments.PostId,COUNT(PostComments.PostId) as tot from PostComments Group by PostComments.PostId)c on b.PostId = c.PostId 
order by a.VoucherWonDate,c.tot 

如何寫這個相同Linq中

+1

您可以添加您期望的任何樣本輸入和輸出嗎?它很難理解你的數據庫表結構。 – 2014-11-25 09:35:12

+0

@RahulSingh是我的問題,仍然令人困惑?請檢查我編輯的問題。 – 2014-11-25 10:38:27

+0

@JeffMercado你有任何解決方案嗎? – 2014-11-25 10:56:02

回答

1

從數據庫結構的外觀,你不需要組通過。只需按VoucherWonDate降序,然後按相關PostPostComments

嘗試類似:

var voucherQuestionTotalCollection = 
this.GetDbSet<Model.VoucherQuestionCollection>() 
.Where(p => p.UserId != userId) 
.OrderByDescending(p => p.VoucherWonDate) 
.ThenBy(p => p.Post.SelectMany(q => q.PostComments).Count()); 

我已經包括了從查詢這是不是在問題中提到的where子句。

+0

謝謝你的回答。在我的問題中,我的加入如何與您的建議一起工作?我有三張桌子,一張桌子(PostComment)被加入,我需要它的數量,如何得到它。我認爲爲了得到這個數字,我們需要在我的linq中使用Group by子句。你能解釋一下嗎? – 2014-11-26 05:48:04

+1

@NithinPaul不,計數也可以通過內嵌在SELECT子句中的子查詢來實現,該子查詢可能將從此LINQ語句(如果這是實體框架)生成。該消息是:如果可能,請在LINQ中使用導航屬性,而不是'join'。唯一的是,應該是'p.Post.SelectMany(p1 => p1.PostComments).Count()'。 – 2014-11-26 07:43:05

+0

@GertArnold謝謝我會檢查它 – 2014-11-26 07:54:14