var query = (from a in context.OQC_INSPECTION_SAMPLE.Where(expression).Where(a => context.OQC_INSPECTION_SAMPLE_ITEM.Where(b => b.SAMPLE_INSPECTION_ID == a.ID).Where(expression2).Any()) join b in context.OQC_INSP_SAMPLE_RPT.Where(expression1) on a.ID equals b.REF_SAMPLE_ID into gc where a.STATE == StateConverter.Active && a.ORG_ID == standard.ORG_ID from gci in gc.DefaultIfEmpty() select new { Str_Array = a.ID + "#" + a.PROD_ID + "#" + a.INSPECTION_NO + "#" + a.WORK_ORDER_ID + "#" + a.PROD_MODEL_ID + "#" + a.TEAM_GROUP, a.APPROVER, a.BARCODE, a.BARCODE_COMPLETION_TIME, a.BATCH_NO, a.CLIENT_NAME, a.CLIENT_ORDER_ID, a.COMPLETE_COUNT, a.DATETIME_CREATED, a.DATETIME_MODIFIED, a.DATETIME_REPORT_GENERATED, a.ELT_RESULT, a.FAIL_COUNT, a.FAIL_DESC, a.FEATURES_LEVEL, a.GROSS_WEIGHT, a.ID, a.INSPECTION_COUNT, a.INSPECTION_LEVEL, a.INSPECTION_NO, a.INSPECTION_RESULT, a.INSPECTION_TIME, a.INSPECTOR, a.IS_CHANGESTD1, a.IS_INSPECTOR, a.MANTISSA, a.NET_WEIGHT, a.ORG_ID, a.PROD_DESC, a.PROD_MODEL_ID, a.PROD_ID, a.PRODUCTION_END_TIME, a.PRODUCTION_LINE, a.PRODUCTION_START_TIME, a.RECEIPTS_TIME, a.RECTIFY, a.RECTIFY_OPINION, a.REMADE_INSPECTION_NO, a.REMADE_REPORT_NO, a.REMADE_RESULT, a.REMARK, a.SAMPLE_COUNT, a.SAMPLING_PERCENT, a.SAMPLING_STANDARD, a.SN, a.SOLD_TO, a.SOLD_TO_DISPLAY, a.STANDARD_NAME, a.STATE, a.STATUS, a.STOP_PRODUCTION, a.STRICT_LEVEL, a.TEAM_GROUP, a.USER_CREATED, a.USER_CREATED_NAME, a.USER_MODIFIED, a.USER_MODIFIED_NAME, a.WORK_ORDER_ID, a.WORK_ORDER_QTY, a.WORKSHOP, a.PRODUCT_INFO, rpt_id =gci==null?"": gci.ID, joinstate = gci==null?"N":(gci.JOINSTATE == null ? "N" : gci.JOINSTATE), }); totalRecords = query.Count(); return query.OrderByDescending(a => new { a.DATETIME_CREATED, a.DATETIME_MODIFIED }) .Skip(viewQueryModel.start).Take(viewQueryModel.length).ToList(); }