This is a migrated thread and some comments may be shown as answers.

where condition error

3 Answers 44 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Burhan Eyimaya
Top achievements
Rank 1
Burhan Eyimaya asked on 05 Mar 2010, 12:43 PM
Hello,

I am trying to filter the result of a linq query based on given search criteria.
But during the filter by using where conditions query returns null.
The method is below.

public List<ExtremeModel.PatientStudy> ReadPatientStudy2(SearchCriteria critera)  
        {  
            IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope();  
            var query = (from p in scope.Extent<Patient>()  
                         join s in scope.Extent<Study>() on p.Id equals s.PatientId  
                         select new  
                         {  
                             StudyStatus = s.Status,  
                             PatientName = p.PatientsName,  
                             NumImages = s.ImagesCount,  
                             StudyDateTime = s.Studytime,  
                             StudyDesc = s.StudyDescription,  
                             PatBirthDate = p.PatientsBirthDate,  
                             PatSex = p.PatientSex,  
                             RefPhysician = s.ReferringPhysiciansName,  
                             StudyIuid = s.StudyInstanceUid,  
                             PatientId = p.Id,  
                             StudyId = s.Id  
                         });  
 
            // Conditions  
            if(critera.PatientId != 0)  
                query = query.Where(ps => ps.PatientId == critera.PatientId);  
 
            if(!string.IsNullOrEmpty(critera.PatientName))  
                query = query.Where(ps => ps.PatientName.Contains(critera.PatientName));  
 
            if (critera.StudyId != 0)  
                query = query.Where(ps => ps.StudyId == critera.StudyId);  
 
            if (critera.StudyStatusList != null)  
            {  
                ObservableCollection<int> studyStatusList = critera.StudyStatusList;  
                int?[] sList = new int?[studyStatusList.Count];  
                for (int x = 0; x < studyStatusList.Count; x++)  
                    sList[x] = studyStatusList[x];  
 
                query = query.Where(ps => sList.Contains(ps.StudyStatus));  
            }  
 
            // Get list  
            List<ExtremeModel.PatientStudy> list = new List<ExtremeModel.PatientStudy>();  
            foreach (var ps in query)  
            {  
                ExtremeModel.PatientStudy patientStudy = new ExtremeModel.PatientStudy();  
                patientStudy.StudyStatus = ps.StudyStatus;  
                patientStudy.PatName = ps.PatientName;  
                patientStudy.NumInstances = ps.NumImages;  
                patientStudy.StudyDatetime = ps.StudyDateTime;  
                patientStudy.StudyDesc = ps.StudyDesc;  
                patientStudy.StudyDesc = ps.StudyDesc;  
                patientStudy.PatBirthdate = ps.PatBirthDate.ToString();  
                patientStudy.PatSex = ps.PatSex.ToString();  
                patientStudy.RefPhysician = ps.RefPhysician;  
                patientStudy.StudyIuid = ps.StudyIuid;  
                patientStudy.PatientID = ps.PatientId;  
                patientStudy.StudyID = ps.StudyId;  
 
                list.Add(patientStudy);  
            }  
 
            return list;  
        } 

If one of the conditions that filters the query runs it results an error. Which part I am doing wrong.
Please help.

Burhan Eyimaya
Thanks

3 Answers, 1 is accepted

Sort by
0
Zoran
Telerik team
answered on 15 Mar 2010, 05:19 PM
Hello Burhan Eyimaya,

The reason for your issues is that you are appending the where clauses after the select clause in your query. That kind of queries are not correctly processed with the current Linq implementation of Telerik OpenAccess ORM. As a workaround you could do some re-factoring on your method so that you apply the where clauses before making the join and select.

Here is an example of the possible implementation:
var patientQuery = scope.Extent<Patient>();
var studyQuery = scope.Extent<Study>();
 
 
 
      if(critera.PatientId != 0) 
        patientQuery = query.Where(ps => ps.PatientId == critera.PatientId); 
 
    if(!string.IsNullOrEmpty(critera.PatientName)) 
        patientQuery = query.Where(ps => ps.PatientName.Contains(critera.PatientName)); 
 
    if (critera.StudyId != 0) 
        studyQuery = query.Where(ps => ps.StudyId == critera.StudyId); 
 
    if (critera.StudyStatusList != null
    
        ObservableCollection<int> studyStatusList = critera.StudyStatusList; 
        int?[] sList = new int?[studyStatusList.Count]; 
        for (int x = 0; x < studyStatusList.Count; x++) 
            sList[x] = studyStatusList[x]; 
 
        studyQuery = query.Where(ps => sList.Contains(ps.StudyStatus)); 
    }
 
      var queryFinal = (from p in patientQuery
               join s in sstudyQuery on p.Id equals s.PatientId 
               select new 
                 
                     StudyStatus = s.Status, 
                     PatientName = p.PatientsName, 
                     NumImages = s.ImagesCount, 
                     StudyDateTime = s.Studytime, 
                     StudyDesc = s.StudyDescription, 
                     PatBirthDate = p.PatientsBirthDate, 
                     PatSex = p.PatientSex, 
                     RefPhysician = s.ReferringPhysiciansName, 
                     StudyIuid = s.StudyInstanceUid, 
                     PatientId = p.Id, 
                     StudyId = s.Id 
                 });



All the best,
Zoran
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 28 Mar 2010, 04:35 PM
Are there plans to add that functionality?
0
Zoran
Telerik team
answered on 30 Mar 2010, 01:48 PM
Hi Steve,

 Yes, at the moment we are doing some refactoring of the Linq engine in OpenAccess which will allow to write such and other more complex queries for the future.

Best wishes,
Zoran
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
LINQ (LINQ specific questions)
Asked by
Burhan Eyimaya
Top achievements
Rank 1
Answers by
Zoran
Telerik team
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Share this question
or