【问题标题】:Lambda concatenationLambda 连接
【发布时间】:2017-08-08 14:04:21
【问题描述】:

我尝试将我的 lambda 字符串与 + 符号一起放入,但它不会让我运行代码,因为它说表达式无效!

代码顶部

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

【问题讨论】:

  • 什么时候可以将简单字符串与实际代码连接起来?
  • 您试图将Expressionstring 连接起来,但您不能。
  • 那你会怎么做呢?我只是个实习生。
  • 查看发布的答案。这就是你想要处理它的方式。如果您绝对需要使用字符串(即,您正在从外部源读取数据),您可以使用反射来完成此操作。
  • 文体小问题:你不应该明确比较truefalse

标签: c# sql lambda asp.net-core


【解决方案1】:

所有这些 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);
}

【讨论】:

【解决方案2】:

审批过滤器需要是 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 =&gt; true

【讨论】:

    【解决方案3】:

    为确保它适用于 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));
    

    “结果”变量将获得您想要的结果。

    【讨论】:

    • 您不应该将AsEnumerable 用于这些操作,因为您希望它们在数据库上而不是在内存中执行。如果您想避免显式键入变量,可以使用AsQueryable
    猜你喜欢
    • 1970-01-01
    • 2019-01-22
    • 2014-05-13
    • 2011-09-09
    • 1970-01-01
    • 2018-09-22
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多