开发者

Building where clause for LINQ query in WCF data service

I have a WebGet that is passed one or more provider codes. For each provider code, I need to return a patient list for all inpatients where their code exists as admitting,attending or consulting provider.

Should be:

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

The "for" loop is not appending the criteria to the where clause. What am I doing wrong??

Thanks

    [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.Equal开发者_StackOverflow中文版s(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;
        }
    }


I think you may need to put the results of the query into a temporary storage variable (e.g. List<vw_patient>) and then append that to a master list. Something like:-

As it stands, you are overwriting the results variable on every iteration of the loop

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


In place of

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

You can do:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜