private void btnFind_Click(object sender, EventArgs e) { string whereClause = string.Empty; EF6.Staff staff = null; if (!txtStaff.Text.Trim().Equals("*")) { staff = StaffEx.GetByStaffNumber(txtStaff.Text.Trim()); } var smartTag4Staff = SmartTag4StaffEx.GetByTagCode("HKID"); if (!txtHKID.Text.Trim().Equals("*")) { if (smartTag4Staff != null) { if (staff != null) { whereClause = " StaffId = '" + staff.StaffId.ToString() + "' AND TagId = '" + smartTag4Staff.TagId.ToString() + "' AND SmartTagValue = '" + txtHKID.Text.Trim() + "'"; } else { whereClause = " TagId = '" + smartTag4Staff.TagId.ToString() + "' AND SmartTagValue = '" + txtHKID.Text.Trim() + "'"; } } } else { if (smartTag4Staff != null) { if (staff != null) { whereClause = " StaffId = '" + staff.StaffId.ToString() + "' AND TagId = '" + smartTag4Staff.TagId.ToString() + "'"; } else { whereClause = " TagId = '" + smartTag4Staff.TagId.ToString() + "'"; } } } using (var ctx = new EF6.RT2020Entities()) { var list = ctx.StaffSmartTag.SqlQuery( String.Format( "Select * from StaffSmartTag Where {0}", String.IsNullOrEmpty(whereClause) ? "1 = 1" : whereClause )) .AsNoTracking() .ToList(); if (list.Count > 0) { int iCount = 1; foreach (var item in list) { ListViewItem objItem = this.lvStaffList.Items.Add(iCount.ToString()); if (staff == null) { var sta = StaffEx.GetByStaffId(item.StaffId); if (sta != null) { objItem.SubItems.Add(sta.StaffNumber); } } else { objItem.SubItems.Add(txtStaff.Text.Trim()); } objItem.SubItems.Add(item.SmartTagValue); iCount++; } } } }