2017-08-08 3 views
-4

Ich habe versucht, meine Lambda-Zeichenfolge mit + Zeichen zu setzen, und es wird mich nicht den Code ausführen lassen, weil es ungültigen Ausdruck sagt!Lambda-Verkettung

Top of 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; 
     } 

Filter

// 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 = ""; 
} 

Abfrage

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

Da zuordnen, wenn Sie einfache Zeichenfolgen mit den tatsächlichen Code verketten? –

+1

Sie versuchen, 'Expression' mit' string' zu verketten, können Sie nicht. – tchelidze

+0

Dann, wie würdest du das tun? Ich bin nur Praktikant. –

Antwort

1

Der Genehmigungsfilter muss ein Lambda-Ausdruck und keine Zeichenfolge sein. So bauen Sie es.

// 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; 
} 

Und das ist, wie Sie es in der Hauptabfrage verwenden.

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)); 

Wenn die Genehmigung keine der 3 überprüft werden kann dann Standardwert i => true

5

Alle diese Linq Methoden zurückkehren IQueryable<>, die tatsächlich gegen die Datenbank nicht ausgeführt werden, bis Sie iterieren über die Ergebnisse. So kann man einfach etwas tun:

//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

Kommentare sind nicht für längere Diskussionen; Diese Konversation wurde [in den Chat verschoben] (http://chat.stackoverflow.com/rooms/151406/discussion-on-answer-by-davidg-lambda-concatenation). –

-2

Um sicherzustellen, dass es funktioniert mit IEnumerable und IQueryable können Sie den folgenden Code verwenden:

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)); 

Das „Ergebnis“ Variable wird das gewünschte Ergebnis haben .

+0

Sie sollten 'AsEnumerable' nicht für diese Operationen verwenden, da sie in der DB ausgeführt werden sollen, nicht im Speicher. Sie können 'AsQueryable' verwenden, wenn Sie die Variable nicht explizit eingeben wollen. – Servy

+0

Es funktioniert nicht wegen meiner Rückkehr –