【问题标题】:Unable to create a constant value of type 'x'. Only primitive types or enumeration types are supported in this context无法创建类型为“x”的常量值。此上下文仅支持原始类型或枚举类型
【发布时间】:2020-10-08 19:45:51
【问题描述】:

我想从 visaConfigurationDocuments 表中获取 VisaCodeIds 列表并与 visaCodes 表一起获取 VisaCodeName 和 我收到以下查询的此错误:

     public List<VisaCodesNamesDto> GetVisaCodesNames(List<VisaCodesNamesInputDto> input)
    {


        var result = (from visaConfigurationDocuments in Context.VisaConfigurationDocuments
                      join visaCodes in Context.VisaCodes
                      on visaConfigurationDocuments.VisaCodeId equals visaCodes.VisaCodeId
                      where (input.Any(x => x.VisaId == visaConfigurationDocuments.VisaId && x.VersionNo == visaConfigurationDocuments.VersionNo))
                      select new VisaCodesNamesDto
                      {
                          VisaCodeName = visaCodes.NameAr,
                          VisaId = visaConfigurationDocuments.VisaId,
                          VersionNo = visaConfigurationDocuments.VersionNo
                      }).ToList();

        return result;
    }
}

【问题讨论】:

  • 好吧,考虑一下如何将 LINQ 转换为 SQL。什么 SQL 代码将表示 List&lt;VisaCodesNamesInputDto&gt; 类型的非数据库对象?然后考虑错误消息的文本。我相信你会拼凑起来的。
  • 我知道问题,但如何有效地解决它
  • 您可以在查询中运行单个变量 where 子句,然后在返回结果后按内存中的第二个变量进行过滤。

标签: c# entity-framework linq asp.net-web-api


【解决方案1】:
public List<VisaCodesNamesDto> GetVisaCodesNames(List<VisaCodesNamesInputDto> input) {
    var ids = input.Select(i => i.VisaId).ToList();
    // filter by a single variable...
    var result = Context.VisaConfigurationDocuments
        .Where(vcd => ids.Contains(vcd.VisaId))
        .Select(vcd => new {
            VisaCodeName = visaCodes.NameAr,
            VisaId = visaConfigurationDocuments.VisaId,
            VersionNo = visaConfigurationDocuments.VersionNo
        })
        .ToList();

    // now filter by the second variable...
    result = result
        .Where(dto => input.Any(i => i.VisaId == dto.VisaId && i.VersionNo == dto.VersionNo)
        .ToList();

    return result;
}

【讨论】:

    【解决方案2】:
            public List<VisaCodesNamesDto> GetVisaCodesNames(List<VisaCodesNamesInputDto>input)
        {
    
    
            List<string> filterlist = input.Select(x => x.VisaId.ToString() + "-" + x.VersionNo).ToList();
    
    
            var result = (from visaConfigurationDocuments in Context.VisaConfigurationDocuments
                          join visaCodes in Context.VisaCodes
                          on visaConfigurationDocuments.VisaCodeId equals visaCodes.VisaCodeId
                          where filterlist.Any(x => x == visaConfigurationDocuments.VisaId.ToString() + "-" + visaConfigurationDocuments.VersionNo)
                          select new VisaCodesNamesDto
                          {
                              VisaCodeNameAr = visaCodes.NameAr,
                              VisaCodeNameEn = visaCodes.NameEn,
                              VisaId = visaConfigurationDocuments.VisaId,
                              VersionNo = visaConfigurationDocuments.VersionNo
                          }).ToList();
    
            return result;}
    

    我做了一个解决方案来解决我在 where 子句中转换复杂对象类型的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 2013-04-30
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多