簡體   English   中英

在Linq中唯一地從每個組中獲取計數最高的記錄

[英]Fetch records with highest count from each group uniquely in Linq

我只需要每個組中具有最大計數的記錄。 必須根據具有最大記錄計數的選區將結果分組。這是代碼。

protected void Page_Load(object sender, EventArgs e)
{
   var query1 = from m in db.Votes 
                group m by m.CandidateID
                into g
                let w= g.ToArray().Count()
                orderby w descending
                select new {CFN=
                                (from a in db.Persons where a.PersonID==((from j in db.Candidates
                                 from u in db.Persons
                                 where j.PersonID==u.PersonID && j.CandidateID==g.Key
                                 select u.PersonID).Single())select a.PersonFirstName ).Single(),
                            CMN =
                                (from a in db.Persons
                                 where a.PersonID == ((from j in db.Candidates
                                                       from u in db.Persons
                                                       where j.PersonID == u.PersonID && j.CandidateID == g.Key
                                                       select u.PersonID).Single())
                                select a.PersonMiddleName).Single(),
                           CLN =
                                (from a in db.Persons
                                 where a.PersonID == ((from j in db.Candidates
                                                       from u in db.Persons
                                                       where j.PersonID == u.PersonID && j.CandidateID == g.Key
                                                       select u.PersonID).Single())
                                select a.PersonLastName).Single(),
                           PName=
                                 (from b in db.Parties
                                   where b.PartyID==((from c in db.Candidates
                                                      from d in db.Parties
                                                      where c.PartyID==d.PartyID && c.CandidateID==g.Key
                                                      select d.PartyID).Single())
                                   select b.Name).Single(),
                           ConName=
                                (from d in db.Candidates
                                 where d.CandidateID==g.Key
                                 select d.ConstituencyName).Single()
                            ,VC=w};

    foreach (var pair in query1)
    {
        TableRow row = new TableRow();
        TableCell cell1 = new TableCell();
        cell1.Style.Value = "text-align:center";
        cell1.Text = pair.CFN+" "+pair.CMN+" "+pair.CLN;
        TableCell cell2 = new TableCell();
        cell2.Style.Value = "text-align:center";
        cell2.Text = pair.PName;
        TableCell cell3 = new TableCell();
        cell3.Style.Value = "text-align:center";
        cell3.Text = pair.VC.ToString();
        TableCell cell4 = new TableCell();
        cell4.Style.Value = "text-align:center";
        cell4.Text=pair.ConName;
        row.Cells.Add(cell1);
        row.Cells.Add(cell2);
        row.Cells.Add(cell3);
        row.Cells.Add(cell4);

        table1.Rows.Add(row);

    }

}

我得到以下輸出

Candidate Name  Party Name  Votes   Constituency
C1                     P1            12       C1
C2                     P2             5       C2
C3                     P1             3       C1

我想要以下輸出

Candidate Name  Party Name  Votes   Constituency
C1                     P1            12       C1
C2                     P2             5       C2

最后一條記錄不應出現,因為它屬於先前的選區。

在我看來,您目前根本不應該按選區分組。 下面的查詢將幫助您入門。

var query1 = from vote in db.Votes
             group vote by vote.CandidateID into g
             select new { CandidateID = g.Key, Count = g.Count() } into voteCount
             join candidate in db.Candidates
             on voteCount.CandidateID equals candidate.CandidateID
             group voteCount by candidate.Constituency into constituencies
             select constituencies.OrderByDescending(v => v.Count).First()
             // Rest of query

第一個塊首先對每個候選人的票數進行計數,然后按選區將這些候選人/計數對分組,然后僅選擇第一個。 因此,這是一個候選/計數對的序列,每個選區只有最上面的一對。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM