在 WCF 数据服务中为 LINQ 查询生成 where 子句



我有一个传递了一个或多个提供程序代码的WebGet。 对于每个提供者代码,我需要返回所有住院患者的患者列表,其中其代码存在为入院、主治或咨询提供者。

应该是:

WHERE (inpatient = 1)
AND (dischargeddate IS NULL)
AND ((attending = 'SMITH') OR (admitting = 'SMITH') OR (consulting = 'SMITH'))

"for"循环不会将条件附加到 where 子句。 我做错了什么??

谢谢

    [WebGet]
    public IQueryable<vw_patient> GetInpatients(string providercode, string searchtext)
    {
        string[] providers = providercode.Split(new char[] { '|' });
        if (string.IsNullOrEmpty(searchtext))
        {
            var results = (from p in this.CurrentDataSource.vw_patient
                            where p.inpatient.Equals(true)
                            && p.dischargedate.Equals(null)
                            select p);
            foreach (string provider in providers)
            {
                results = results.Where<vw_patient>(e => e.attending.Equals(provider) || e.admitting.Equals(provider) || e.consulting.Contains(provider));
            }
            results.OrderBy(p => p.roomloc).ThenByDescending(p => p.patientname);
            return results;
        }
        else
        {
            var results = (from p in this.CurrentDataSource.vw_patient
                            where p.patientname.StartsWith(searchtext)
                            && p.inpatient.Equals(true)
                            && p.dischargedate.Equals(null)
                            select p);
            foreach (string provider in providers)
            {
                results = results.Where<vw_patient>(e => e.attending.Equals(provider) || e.admitting.Equals(provider) || e.consulting.Contains(provider));
            }
            results.OrderBy(p => p.roomloc).ThenByDescending(p => p.patientname);
            return results;
        }
    }

我认为您可能需要将查询结果放入临时存储变量中(例如 List<vw_patient> ),然后将其追加到主列表。像这样:-

就目前而言,您将在循环的每次迭代中覆盖results变量

List<vw_patient> fullList = new List<vw_patient>();
foreach (string provider in providers){
    results = results.Where<vw_patient>(
        e => e.attending.Equals(provider) || 
             e.admitting.Equals(provider) ||
             e.consulting.Contains(provider)
    );
    fullList.AddRange(result.ToList<vw_patient>());
} 

代替

foreach (string provider in providers)
{
  results = results.Where<vw_patient>(e => e.attending.Equals(provider) || e.admitting.Equals(provider) || e.consulting.Contains(provider));
}

你可以做:

results.Where(e => providers.Contains(e.attending) || providers.Contains(e.admitting) || providers.Contains(e.admitting));

相关内容

  • 没有找到相关文章