2017-08-08 75 views
-4

我試圖把我的lambda字符串與+標誌放在一起,它不會讓我運行代碼,因爲它說無效表達式!Lambda concatenation

頂端Code

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Mvc; 
using Microsoft.AspNetCore.Mvc.Rendering; 
using Microsoft.EntityFrameworkCore; 
using Certifications.Data; 
using Certifications.Models; 
using Microsoft.EntityFrameworkCore.Internal; 

namespace Certifications.Controllers 
{ 
    public class Managerial : Controller 
    { 
     private readonly CertificationContext _context; 

     public Managerial(CertificationContext context) 
     { 
      _context = context; 
     } 

過濾

// Approval Filter 
string ApprovalFilterBuild = ""; 

if (approval == "Approved") 
{ 
    ApprovalFilterBuild = ".Where(i => i.Approved == true);"; 
} 

if (approval == "Revoked") 
{ 
    ApprovalFilterBuild = ".Where(i => i.Approved == false);"; 
} 

if (approval == "ALL") 
{ 
    ApprovalFilterBuild = ""; 
} 

查詢

var certificationContext = _context.INT_CertificationsXREF 
      .Include(i => i.INT_CertificationCategories) 
      .Include(i => i.INT_Certifications) 
      .Include(i => i.INT_CertificationConferred) 
      .Include(i => i.RIM_Resource) 
      +ApprovalFilterBuild+ 
      .Where(i => i.RIM_Resource.LAN == i.RIM_Resource.LAN) 
      .Where(i => LANlist.Contains(i.RIM_Resource.LAN)); 


return View(await certificationContext.ToListAsync()); 
+9

既然你什麼時候可以Concat的實際代碼簡單的字符串? –

+1

您試圖將'Expression'與'string'連接起來,但不能。 – tchelidze

+0

那麼你會如何做到這一點?我只是一名實習生。 –

回答

1

批准過濾器需要是lambda表達式而不是字符串。這是你如何建立它。

// Approval Filter 
Expression<Func<INT_CertificationsXREF, bool>> ApprovalFilterBuild; 

if (approval == "Approved") 
{ 
    ApprovalFilterBuild = i => i.Approved == true; 
} 

if (approval == "Revoked") 
{ 
    ApprovalFilterBuild = i => i.Approved == false; 
} 

if (approval == "ALL") 
{ 
    ApprovalFilterBuild = i => true; 
} 

這就是你如何在主查詢中使用它。

var certificationContext = _context.INT_CertificationsXREF 
    .Include(i => i.INT_CertificationCategories) 
    .Include(i => i.INT_Certifications) 
    .Include(i => i.INT_CertificationConferred) 
    .Include(i => i.RIM_Resource) 
    .Where(ApprovalFilterBuild) 
    .Where(i => i.RIM_Resource.LAN == i.RIM_Resource.LAN) 
    .Where(i => LANlist.Contains(i.RIM_Resource.LAN)); 

如果批准,可沒有3個檢查,然後考慮將默認值i => true

5

所有這些LINQ的方法返回IQueryable<>實際上並不執行對您的數據庫中,直到你迭代結果。所以,你可以簡單地做這樣的事情:

//Assuming the type name is INT_CertificationsXREF: 
IQueryable<INT_CertificationsXREF> certificationContext = _context.INT_CertificationsXREF 
    .Include(i => i.INT_CertificationCategories) 
    .Include(i => i.INT_Certifications) 
    .Include(i => i.INT_CertificationConferred) 
    .Include(i => i.RIM_Resource); 

if (approval == "Approved") 
{ 
    certificationContext = certificationContext.Where(i => i.Approved == true); 
} 
else if (approval == "Revoked") 
{ 
    certificationContext = certificationContext.Where(i => i.Approved == false); 
} 
+0

評論是不適合擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/151406/discussion-on-answer-by-davidg-lambda-concatenation)。 –

-2

要確保它與兩個IEnumerable和IQueryable的,你可以使用下面的代碼:

var certificationContext = _context.INT_CertificationsXREF 
     .Include(i => i.INT_CertificationCategories) 
     .Include(i => i.INT_Certifications) 
     .Include(i => i.INT_CertificationConferred) 
     .Include(i => i.RIM_Resource).AsEnumerable(); 

if (approval == "Approved") 
{ 
    certificationContext = certificationContext.Where(i => i.Approved).AsEnumerable(); 
} 
if (approval == "Revoked") 
{ 
certificationContext = certificationContext.Where(i => !i.Approved).AsEnumerable(); 
} 
var result = certificationContext.Where(i => i.RIM_Resource.LAN == i.RIM_Resource.LAN).Where(i => LANlist.Contains(i.RIM_Resource.LAN)); 

的「結果」變量將有你想要的結果。

+0

對於這些操作,您不應該使用'AsEnumerable',因爲您希望它們在數據庫上執行,而不是在內存中執行。如果你想避免明確輸入變量,你可以使用'AsQueryable'。 – Servy

+0

它不會因爲我的回報而工作 –