Linq to entities - 通过包含

时间:2015-05-28 07:36:32

标签: c# linq entity-framework linq-to-entities

我在两张桌子之间有一对多的货物。我正在使用实体框架6.表1称为“投票”,表2是“投票信息”。 Vote表有一个名为“VoteYear”的属性,VoteInfo表有一个名为“VoterId”的属性。某一年可能会有很多投票记录,但选民只能在每次投票记录中投票一次。我的目标是返回所有选票的清单 在选民投票的一年中。我想将VoteInfo表中的信息包含在此列表中。这就是我想要返回结果的方式:

<Vote>
<VoteDate>5/21/2015</VoteDate>
<VoteYear>2015</VoteYear>
<VoteInfo>
  <VoterId>1</VoterId>
  <VoteValue>Yes</VoteValue>
</VoteInfo>
</Vote>
<Vote>
<VoteDate>4/21/2015</VoteDate>
<VoteYear>2015</VoteYear>
<VoteInfo>
  <VoterId>1</VoterId>
  <VoteValue>Yes</VoteValue>
</VoteInfo>
</Vote>
<Vote>
<VoteDate>3/21/2015</VoteDate>
<VoteYear>2015</VoteYear>
<VoteInfo>
  <VoterId>1</VoterId>
  <VoteValue>No</VoteValue>
</VoteInfo>
</Vote>

我试过这个但没有工作:

public IQueryable<Vote> GetVoterRecord(string Year, string VoterId)
{
   return _dbCtx.Vote.Include("VoteInfo")
  .Where(v => v.VoteYear == Year && v.VoteInfo.Any(i => i.VoterId == VoterId));
}

1 个答案:

答案 0 :(得分:0)

满足OP条件的示例代码如下。

public class VotesContext : DbContext {
        public DbSet<Vote> Votes { get; set; }
        public DbSet<VoteInfo> VoteInfos { get; set; }
    }

    public class Vote {
        public int VoteId { get; set; }
        public string VoteYear { get; set; }
        public virtual ICollection<VoteInfo> VoteInfo { get; set; }
    }

    public class VoteInfo {
        public int VoteInfoId { get; set; }
        public string VoterId { get; set; }
    }

然后是代码

            var v1 = db.Votes.Add(new Vote { VoteYear = "2001", });
            var v2 = db.Votes.Add(new Vote { VoteYear = "2001", });
            var v3 = db.Votes.Add(new Vote { VoteYear = "2002", });
            var v4 = db.Votes.Add(new Vote { VoteYear = "2003", });

            v1.VoteInfo = new List<VoteInfo> { new VoteInfo { VoterId = "1", }, new VoteInfo { VoterId = "3", }, }; 
            v2.VoteInfo = new List<VoteInfo> { new VoteInfo { VoterId = "1", }, new VoteInfo { VoterId = "4", }, }; 
            v3.VoteInfo = new List<VoteInfo> { new VoteInfo { VoterId = "2", }, }; 
            //v1.VoteInfo = new List<VoteInfo> { new VoteInfo { VoterId = "3", }, }; 
            //v2.VoteInfo = new List<VoteInfo> { new VoteInfo { VoterId = "4", }, };

            db.SaveChanges();

            // Each record corresponds to a vote in 2001 where voter #1 has voted.
            // VoteInfo for each voter in this vote is included.
            var votesIn2001With1 = db.Votes
                .Where(x1 => 
                    x1.VoteYear == "2001" &&
                    x1.VoteInfo.Any(x2 => x2.VoterId == "1"))
                .Select(x => new {
                    vote = x,
                    voteInfo = x.VoteInfo.ToList(),
                })
                .ToList();

            // Each record corresponds to a vote in 2001 where voter #1 has voted.
            // VoteInfo only for voter #1 is included.
            var votesOf1In2001 = db.Votes
                .Where(x1 => 
                    x1.VoteYear == "2001" &&
                    x1.VoteInfo.Any(x2 => x2.VoterId == "1"))
                .Select(x1 => new {
                    vote = x1,
                    voteInfo = x1.VoteInfo
                        .Where(x2 => x2.VoterId == "1")
                        .ToList(),
                })
                .ToList();

完成必要的事情。