2017-06-03 60 views
0
var data11 = (from p in db.FeePlan.Where(d => d.AcademicYearID == dts.AcademicYearID && d.Active == true && d.IsEffectiveDates == false) 
       from fb in db.FeePlansToBranches.Where(x => x.FeePlanID == p.FeePlanID && x.BranchID == BranchID) 
       from b in db.Branches.Where(b => b.BranchID == fb.BranchID) 
       from fp in db.AssignFeeplanToClasses.Where(a => a.BranchID == BranchID && a.ClassID == ClassID && a.FeePlanID == p.FeePlanID) 
       from cl in db.Classes.Where(c => c.ClassID == fp.ClassID) 
       select new 
        { 
         p.FeePlanName, 
         p.FeePlanID, 
         b.BranchName, 
         cl.ClassName, 
         fp.AssignFeeplanToClassID, 
         fp.CreatedDate 
        }).Distinct().ToList(); 

var data2 = (from p in db.FeePlan.Where(d => d.AcademicYearID == dts.AcademicYearID && d.Active == true &&d.IsEffectiveDates == true) 
      from fb in db.FeePlansToBranches.Where(x => x.FeePlanID == p.FeePlanID && x.BranchID == BranchID) 
      from b in db.Branches.Where(b => b.BranchID == fb.BranchID) 
      from fp in db.AssignFeeplanToClasses.Where(a => a.BranchID == BranchID && a.ClassID == ClassID && a.FeePlanID == p.FeePlanID) 
      from cl in db.Classes.Where(c => c.ClassID == fp.ClassID) 
      where (DateTime.Now> p.StartDate && DateTime.Now < p.EndDate) 
      select new 
        { 
         p.FeePlanName, 
         p.FeePlanID, 
         cl.ClassName, 
         b.BranchName, 
         fp.AssignFeeplanToClassID, 
         fp.CreatedDate 
        }).Distinct().ToList(); 

js.Data = data11.Union(data2); 

我是新來的LINQ;請幫助我如何在LINQ查詢中使用union all爲兩個變量存儲結果。如何寫工會都在LINQ兩個LINQ導致

在我使用兩個LINQ查詢,得到的結果上面的代碼,但我想工會存儲在其他變量的兩個結果。請幫幫我。

+0

您可以使用[的AddRange(https://msdn.microsoft.com/en-us/library/z883w3dc(V = vs.110)的.aspx) – bansi

回答

1

如你都轉換集合列表您可以使用清單AddRange方法來做到這一點。

var data11 = (from p in db.FeePlan.Where(d => d.AcademicYearID == dts.AcademicYearID && d.Active == true && d.IsEffectiveDates == false) 
          from fb in db.FeePlansToBranches.Where(x => x.FeePlanID == p.FeePlanID && x.BranchID == BranchID) 
          from b in db.Branches.Where(b => b.BranchID == fb.BranchID) 
          from fp in db.AssignFeeplanToClasses.Where(a => a.BranchID == BranchID && a.ClassID == ClassID && a.FeePlanID == p.FeePlanID) 
          from cl in db.Classes.Where(c => c.ClassID == fp.ClassID) 
          select new 
          { 
           p.FeePlanName, 
           p.FeePlanID, 
           b.BranchName, 
           cl.ClassName, 
           fp.AssignFeeplanToClassID, 
           fp.CreatedDate 
          }).Distinct().ToList().AddRange(

      (from p in db.FeePlan.Where(d => d.AcademicYearID == dts.AcademicYearID && d.Active == true &&d.IsEffectiveDates == true) 
         from fb in db.FeePlansToBranches.Where(x => x.FeePlanID == p.FeePlanID && x.BranchID == BranchID) 
         from b in db.Branches.Where(b => b.BranchID == fb.BranchID) 
         from fp in db.AssignFeeplanToClasses.Where(a => a.BranchID == BranchID && a.ClassID == ClassID && a.FeePlanID == p.FeePlanID) 
         from cl in db.Classes.Where(c => c.ClassID == fp.ClassID) 
         where (DateTime.Now> p.StartDate && DateTime.Now < p.EndDate) 
         select new 
         { 
          p.FeePlanName, 
          p.FeePlanID, 
          cl.ClassName, 
          b.BranchName, 
          fp.AssignFeeplanToClassID, 
          fp.CreatedDate 
         }).Distinct().ToList()); 
+0

如果你想要去的,你可以使用@mjwills方法的LINQ方式 – bansi

+0

你甚至可以刪除最後.ToList()調用,因爲需要的AddRange一個IEnumerable - https://msdn.microsoft.com/en-us/library/z883w3dc(v=vs.110).aspx。 – mjwills

+0

@mjwills你是絕對正確的 – bansi

0
var data11 = (from p in db.FeePlan.Where(d => d.AcademicYearID == dts.AcademicYearID && d.Active == true && d.IsEffectiveDates == false) 
           from fb in db.FeePlansToBranches.Where(x => x.FeePlanID == p.FeePlanID && x.BranchID == BranchID) 
           from b in db.Branches.Where(b => b.BranchID == fb.BranchID) 
           from fp in db.AssignFeeplanToClasses.Where(a => a.BranchID == BranchID && a.ClassID == ClassID && a.FeePlanID == p.FeePlanID) 
           from cl in db.Classes.Where(c => c.ClassID == fp.ClassID) 
           select new 
           { 
            FeePlanName = p.FeePlanName, 
            FeePlanID = p.FeePlanID, 
            ClassName = cl.ClassName, 
            BranchName = b.BranchName, 
            AssignFeeplanToClassID = fp.AssignFeeplanToClassID, 
            CreatedDate = fp.CreatedDate 
           }).ToList(); 

       var data2 = (from p in db.FeePlan.Where(d => d.AcademicYearID == dts.AcademicYearID && d.Active == true && d.IsEffectiveDates == true) 
          from fb in db.FeePlansToBranches.Where(x => x.FeePlanID == p.FeePlanID && x.BranchID == BranchID) 
          from b in db.Branches.Where(b => b.BranchID == fb.BranchID) 
          from fp in db.AssignFeeplanToClasses.Where(a => a.BranchID == BranchID && a.ClassID == ClassID && a.FeePlanID == p.FeePlanID) 
          from cl in db.Classes.Where(c => c.ClassID == fp.ClassID) 
          where (DateTime.Now > p.StartDate && DateTime.Now < p.EndDate) 
          select new 
          { 
          FeePlanName= p.FeePlanName, 
           FeePlanID= p.FeePlanID, 
           ClassName= cl.ClassName, 
          BranchName= b.BranchName, 
           AssignFeeplanToClassID= fp.AssignFeeplanToClassID, 
           CreatedDate= fp.CreatedDate 
          }).ToList(); 

       js.Data = data11.Concat(data2); 
+0

我相信你的清除Distinct()調用可能會導致你的問題(即重複),假設你有目的之前他們。 – mjwills

+0

你可能會發現你也可以刪除兩個ToList()調用。 – mjwills

1

兩種方式:

var data3 = data11.Concat(data2).ToList(); 

或者

var data3 = new List<object>(data1); 
data3.AddRange(data2); 

但因爲你似乎不想重複,你可能仍然要使用union

var data3 = data11.Union(data2).ToList();