c# - LINQ 子表达式不接受变量值

标签 c# entity-framework

我有 3 个不同服务器的 3 个上下文。主要应用程序数据使用 context1 连接到 SQL1。在 Web API 服务中,我从连接到 SQL2 的 context2 异步获取人员记录。它返回 4800 多条记录。这些记录根据 userFacility 进行进一步过滤,这是从 context3 连接到 SQL3 的存储过程的结果。

所有方法都适用于我的带有 Visual Studio 2019 和 IIS Express 的开发计算机。当发布到我们网络上的 IIS 服务器时,只有方法 2 有效。看起来 EF 只喜欢内部表达式中的常量值,而不喜欢变量值。有谁知道可能是什么问题吗?

IEnumerable<PeopleDTO> people= await aHelper.GetPeopleAsync(aRepository, defaultQuarter, criteria);
/* sample Facility property of People values are
(1V01) (402) Togus, ME
(1V01) (518) Bedford, MA
(1V01) (523) Boston, MA
(1V01) (608) Manchester, NH
(1V01) (631) Central Western Massachusetts
(3V12) (587) Eastern Colorado, CO
*/

List<string> userFacilities = new List<string>(){“608”,”518”,”674”, "587","648"};

/* approach 1 works in VS but not in IIS */
       pepole= pepole.Where(x => userFacilities.Any(y=> x.Facility.Contains(y)));

/* approach 2 works in both in VS and IIS */
       pepole= pepole.Where(x => userFacilities.Any(y=> x.Facility.Contains("587")));


/* Approach 3 works in VS but not in IIS */
List<PeopleDTO> viewablePeople = new List<PeopleDTO>();

foreach (PeopleDTO p in people)
{
    foreach (string y in userFacilities)
    {
        /* doesn't work in IIS but works in VS IIS  Express if y is a constant*/
        if (p.Facility.IndexOf(y) >= 0)
        {
            viewablePeople.Add(p); 
            break;
        }
    }
}

return Ok(viewablePeople);
      public partial class PeopleDTO
      {
        [DisplayName("Facility")]
        public string Facility { get; set; } 

        [DisplayName("District")]
        public string District { get; set; }

        [DisplayName("Name")]
        public string Name { get; set; }

        [DisplayName("SSN")]
        public string PTFSSN { get; set; }

        [DisplayName("FSOD SSN")]
        public string FSODSSN { get; set; }

        [DisplayName("Fiscal Period")]
        public string FiscalPeriod { get; set; }

        [DisplayName("Numeric Fiscal Period")]
        public int FiscalPeriodInt { get; set; }

        [DisplayName("Episode")]
        public IEnumerable<EpisodeDTO> Episodes { get; set; }

        public PeopleDTO() {
          Episodes = new List<EpisodeDTO>();
        }

最佳答案

我认为重命名变量 fac 可以解决假设是全局变量的问题,因为我在代码中没有看到它,第一种方法中的插值似乎没有引用任何内容

关于c# - LINQ 子表达式不接受变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68642173/

相关文章:

entity-framework - LinqWhere(p => p.Parent == null) 在自引用表中如何工作?

asp.net-mvc-3 - 在数据库优先方法中使用 DbContext 与 Objectcontext 定义数据注释

c# - Web API 脚手架 + MVC View

C# 如何单元测试/验证类提供的 API 是否是线程安全的?

java - 在 Android 中通过 SSL 使用 WCF 服务

c# - 在 C# 中同步闪烁标签

c# - 使用 Entity Framework 设计模式?

c# - 通过 ScrollViewer 的水平/垂直偏移制作动画

c# - WPF 绑定(bind)找不到引用的绑定(bind)源

c# - 通过 Entity Framework 选择优化方式删除 oracle 中的行