方法一、查询List集合(建议采用)
我们的思路是,
先把数据,用lambda语句,按Contains/IsNullOrEmpty/Equals关键字从数据库文件,提取到类的集合。
然后,再用Linq语句,从类的集合检索数据到另一个集合。
在这里,建议采用这种方法,因为我在PatientInfoHistroryModel类统一定义每个属性都为string,然后直接用ToString()即可。
/// <summary>
/// 查询
/// </summary>
///
public void SearchFunc(string strSearchBoxTextContent)
{
try
{
//每次使用前必须清零
patientInfoHistroryModelVar.Clear();
//先把数据库的数据提取出来,放到集合中。
List<PatientInfo_Table> patientInfoList = dataBase.PatientInfo_Table.Where(u => string.IsNullOrEmpty(strSearchBoxTextContent) || u.PatientKey.ToString().Contains(strSearchBoxTextContent) || u.PatientID.Contains(strSearchBoxTextContent) || u.PatientName.Equals(strSearchBoxTextContent)).ToList();
List<PatientStudy_Table> patientStudyList = dataBase.PatientStudy_Table.Where(u => u.PatientKey < 10).ToList();
//按条件检索集合
List<PatientInfoHistroryModel> list =
(from PI in patientInfoList
where (PI.PatientKey < 1000)
select new PatientInfoHistroryModel()
{
PatientInfo = PI.PatientKey.ToString(),
StudyID = PI.PatientID.ToString(),
PatientName = PI.PatientName.ToString(),
PatientSex = PI.PatientSex.ToString(),
PatientAge = PI.PatientAge.ToString(),
PatientBrith = PI.PatientBirthDate.ToString(),
PatientHeight = PI.PatientHeight.ToString(),
PatientWeight = PI.PatientWeight.ToString(),
RecordSource = PI.PatientSource.ToString(),
//StudyTime = PS.StudyDatetime,
//EquipmentType = PS.StudyPhysician,
//StudyPart = PS.StudyType,
//SequenceAmount = PS.SeriesCount,
StudyTime = PI.PatientAge.ToString(),
EquipmentType = PI.PatientAge.ToString(),
StudyPart = PI.HangFlag.ToString(),
SequenceAmount = PI.HangFlag.ToString(),
StudyStutas = PI.StudyCompleteFlag.ToString(),
SuspendState = PI.HangFlag.ToString(),
FilmPrint = PI.PrintFlag.ToString(),
}).ToList();
patientInfoHistroryModelVar = list;
dataBase.Dispose();
}
catch (Exception e)
{
MessageBox.Show("病人历史记录信息表发生查询错误,错误信息:--------------" + e.ToString());
}
}
1、IsNullOrEmpty方法
string.IsNullOrEmpty(strSearchBoxTextContent)——如果为空,返回true。那么下面这条语句表示可以查询所有记录:
Where(u =>true)
2、 Contains方法
每条记录中的每个数据段(或说列)包含该关键字,都会被检索出来。
3、Equals方法
中文字符串,不能用模糊查询(否则会不准确),比如我检索 “急”,出来下面的的数据:
只能用精准查询,如下面的方法:
u.PatientName.Equals(strSearchBoxTextContent)
方法二、查询IQueryable集合
这种方式稍微有些不同的地方是IQueryable,但不能在Linq语句中,使用.ToString()。
/// <summary>
/// 查询
/// </summary>
/// <param name="whereLambda1"></param>
/// <param name="whereLambda2"></param>
/// <returns></returns>
public ObservableCollection<ListViewModel> GetPatientInfo(System.Linq.Expressions.Expression<Func<PreStudyInfo_Table, bool>> whereLambda1, System.Linq.Expressions.Expression<Func<PatientInfo_Table, bool>> whereLambda2)
{
IQueryable<PreStudyInfo_Table> preStudyInfoList = db.PreStudyInfo_Table.Where(whereLambda1);
IQueryable<PatientInfo_Table> patientInfoList = db.PatientInfo_Table.Where(whereLambda2);
List<ListViewModel> list = (from u in preStudyInfoList
from r in patientInfoList
where u.PatientID == r.PatientID
select new ListViewModel()
{
PatientKey = r.PatientKey,
PreListKey = u.PreListKey,
StudyInstanceUID = u.StudyInstanceUID,
StudyID = u.StudyID,
PatientID = u.PatientID,
PatientName = u.PatientName,
PatientSex = u.PatientSex,
PatientAge = r.PatientAge,
AccessionNumber = u.AccessionNumber,
ApplyStudyTime = u.ApplyStudyTime,
StudyRisCode = u.StudyRisCode,
StudyBodyDesc = u.StudyBodyDesc,
STudyDesc = u.STudyDesc,
ReferDoctor = u.ReferDoctor,
ReferOffice = u.ReferOffice,
PatientSource = r.PatientSource == 0 ? "本地" : "Worklist",
Modality = u.Modality,
StudyTypeName = u.StudyType == 3 ? "住院" : (u.StudyType == 2 ? "急诊" : "门诊"),
PatientBirthDate = u.PatientBirthDate,
PatientHeight = r.PatientHeight,
PatientWeight = r.PatientWeight,
PatientSize = r.PatientSize,
BodySize = u.BodySize,
PrintFlag = r.PrintFlag,
StudyCompleteFlag = r.StudyCompleteFlag,
HangFlag = r.HangFlag,
PatientArchiveFlag = r.PatientArchiveFlag
}).ToList();
db.Dispose();
return new ObservableCollection<ListViewModel>(list);
}