在linq到sql中使用多个连接和聚合函数进行查询

时间:2014-11-29 11:49:02

标签: sql linq join

我有以下SQL查询:

SELECT Voting.Name, COUNT(VotingMembers.Result) as Result, Party.Name
                            FROM Voting 
                                LEFT JOIN VotingMembers ON Voting.Id = VotingMembers.Voting_id 
                                LEFT JOIN Member ON VotingMembers.Member_id = Member.Id
                                LEFT JOIN Party ON Member.Party_Id = Party.Id
                                    WHERE VotingMembers.Result = 1 
                            GROUP BY Voting.Name, Party.Name
                            ORDER BY Voting.Name, Result DESC

有没有人可以帮我将SQL查询转换为linq到sql代码?我写了以下代码:

var result = from v in ctx.Votes
                join vm in ctx.VotingMembers on v.Id equals vm.Voting_id into vMembers
                from x in vMembers.DefaultIfEmpty()
                join m in ctx.Members on x.Voting_id equals m.Id into members
                from y in members.DefaultIfEmpty()
                join p in ctx.Parties on y.Id equals p.Id into parties
                from z in parties.DefaultIfEmpty()
                where x.Result == true
                orderby v.Name descending, z.Name descending
                group v by new { VoteName = v.Name, PartyName = z.Name }
                into res
                select new
                {
                    VoteName = res.Key.VoteName,
                    Result = res.Select(a => a.VotingMembers.Select(q => q.Result).Count()),
                    PartyName = res.Key.PartyName
                };

型号:
http://i.imgur.com/LGuNAsG.png

班级模特:

public class Constituency
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Capacity { get; set; }

    public IList<Result> Results { get; set; }
}

public enum GenderType
{
    Male,
    Female
}

public class Member
{
    private ICollection<Term> _terms;
    private ICollection<VotingMembers> _votingMembers;
    public int Id { get; set; }
    public Person Person { get; set; }
    public Party Party { get; set; }

    public virtual ICollection<Term> Terms
    {
        get { return _terms ?? (_terms = new Collection<Term>()); }
        set { _terms = value; }
    }

    public virtual ICollection<VotingMembers> VotingMembers
    {
        get { return _votingMembers ?? (_votingMembers = new Collection<VotingMembers>()); }
        set { _votingMembers = value; }
    }
}

public class Party
{
    public int Id { get; set; }
    public string Name { get; set; }

    public IList<Member> Members { get; set; }
}

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public GenderType Gender { get; set; }
    public DateTime BirthDate { get; set; }

    public IList<Result> Results { get; set; }
    public IList<Member> Memberships { get; set; }
    public Photo Photo { get; set; }
}

public class Photo
{
    public int Id { get; set; }
    public byte[] Content { get; set; }
    public string Description { get; set; }

    public Person Person { get; set; }
}

public class Result
{
    public int Id { get; set; }
    public int Count { get; set; }
    public Constituency Constituency { get; set; }
    public Person Person { get; set; }
}

public class Term
{
    public int Id { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public IList<Member> Members { get; set; }
}

public class Voting
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IList<VotingMembers> VotingMembers { get; set; }
}

public class VotingMembers
{
    public int Member_id { get; set; }
    public int Voting_id { get; set; }
    public Member Member { get; set; }
    public Voting Voting { get; set; }
    public bool Result { get; set; }
}

关于模型创建方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<VotingMembers>().HasKey(q => new {q.Member_id, q.Voting_id});

        modelBuilder.Entity<VotingMembers>()
            .HasRequired(t => t.Voting)
            .WithMany(t => t.VotingMembers)
            .HasForeignKey(t => t.Voting_id);

        modelBuilder.Entity<VotingMembers>()
            .HasRequired(t => t.Member)
            .WithMany(t => t.VotingMembers)
            .HasForeignKey(t => t.Member_id);


        modelBuilder.Entity<Member>().HasMany<Term>(u => u.Terms).WithMany(e => e.Members).Map(c =>
        {
            c.MapLeftKey("Member_id");
            c.MapRightKey("Term_id");
            c.ToTable("TermMembers");
        });

        modelBuilder.Entity<Person>().HasOptional(p => p.Photo).WithRequired(p => p.Person);
        modelBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>();

        base.OnModelCreating(modelBuilder);
    }

但是有些不对劲。我会提供任何帮助,谢谢!

0 个答案:

没有答案