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