如何优化linq查询?

时间:2014-07-16 05:18:46

标签: c# linq-to-sql

我难以获得每个部门的学生平均分数。 而每个系都有很多院系,每个系都有很多课程,每个课程都有很多学生,每个学生都有很多价值。 我所做的查询,我得到了很多显示相同数据捕获所需的时间限制。 请帮助优化我做的这个查询。

double TotalNilaiMutu = 0;
double JumSKS = 0;
double ipkMhs = 0;
double totalIPK = 0;

var queryDepartemen = (from so in StrukturOrganisasis
                        join dp in Departemens on so.ID equals dp.ID
                        orderby dp.ID
                        select new{dp.ID, so.Inisial, so.Nama}).ToList();

foreach(var departemen in queryDepartemen){
    var queryMayor = (from my in Mayors
                        where my.DepartemenID == departemen.ID && my.StrataID == 2
                        select my.ID).ToList();

    var queryMhs = (from ms in MahasiswaSarjanas
                    where queryMayor.Contains(ms.MayorID) &&
                        (
                            from sm in StatusMahasiswas
                            where
                                (
                                    from ts in TahunSemesters
                                    where ts.TahunAwal == 2013
                                    select ts.ID
                                )
                            .Contains(sm.TahunSemesterID)
                            select sm.NIM
                        )
                    .Contains(ms.NIM)
                    select ms.NIM).ToList();

    ipkMhs = 0;

    foreach(var nim in queryMhs){
        var queryNilai = (from kr in KRS 
                            join hm in HurufMutus on kr.HurufMutuID equals hm.ID
                            join kur in 
                                (
                                    from ku in Kurikulums
                                    join mk in MataKuliahs on ku.MataKuliahID equals mk.ID 
                                    select new {ku.ID, mk.Nama, mk.SKS}
                                ) 
                            on kr.KurikulumID equals kur.ID
                            where kr.NIM==nim
                            select new {
                                nilai = hm.NilaiMutu * kur.SKS,
                                sks   = kur.SKS
                            });

        TotalNilaiMutu = 0;
        JumSKS = 0;
        foreach(var ipk in queryNilai){
            TotalNilaiMutu += ipk.nilai;
            JumSKS += ipk.sks;
        }

        if(double.IsNaN(TotalNilaiMutu/JumSKS)) ipkMhs+=0;
        else ipkMhs += TotalNilaiMutu/JumSKS;

    }
    if(double.IsNaN(ipkMhs/queryMhs.Count())) totalIPK=0;
    else totalIPK=ipkMhs/queryMhs.Count();

    Console.WriteLine(departemen.Nama +" -> "+ totalIPK +" : "+ ipkMhs +" / "+queryMhs.Count());
}

1 个答案:

答案 0 :(得分:-1)

您是否尝试过添加AsParallel()?

     var queryDepartemen = (from so in StrukturOrganisasis.AsParallel()
                    join dp in Departemens on so.ID equals dp.ID
                    orderby dp.ID
                    select new{dp.ID, so.Inisial, so.Nama}).ToList();

  foreach(var departemen in queryDepartemen.AsParallel()){
                    var queryMayor = (from my in Mayors
                    where my.DepartemenID == departemen.ID && my.StrataID == 2
                    select my.ID).ToList();

var queryMhs = (from ms in MahasiswaSarjanas.AsParallel()
                where queryMayor.Contains(ms.MayorID) &&
                    (
                        from sm in StatusMahasiswas
                        where
                            (
                                from ts in TahunSemesters
                                where ts.TahunAwal == 2013
                                select ts.ID
                            )
                        .Contains(sm.TahunSemesterID)
                        select sm.NIM
                    )
                .Contains(ms.NIM)
                select ms.NIM).ToList();