如何根据表A中A列的结果更新表B中的b列

时间:2019-06-21 18:01:28

标签: c# sql

我有一个结果验证应用。将候选结果与某些API结果进行匹配,然后将数据填充到名为result的表中,并将候选数据插入具有状态列的候选表中。

  1. 我想使用CandidateNo检查结果表以查看他是否有不匹配的结果。
  2. 如果为true,则更新候选表的状态并将其设置为未经验证
  3. 否则将其设置为“已验证”

因此,如果候选人的所有科目都等于“匹配”,则updateStatusOfCandidate(d.Candidate.CandidateNo,“ VERIFIED”);

enter code here

否则updateStatusOfCandidate(d.Candidate.CandidateNo,“未验证”);

 private void Process()
    {
        //read the data from csv
        List<CsvData> dataFromCsv = ReadCsvFile();

        //if we dont get any data, lets return and sleep...
        //if(dataFromCsv.Count < 1)
        //{
        //    return;
        //}

        //lets save the data to the db
        SaveCandidateData(dataFromCsv);

        //now lets get the data, from the db, we are doing this because we will need the id of the students
        List<Candidate> listFromDb = dbCtxt.Candidates.Where(c => c.Status == null).ToList();
        List<CsvData> nonMatchedData = new List<CsvData>();
        foreach(var l in listFromDb)
        {
            CsvData csvData = new CsvData();
            csvData.Candidate = l;
            csvData.Result = dbCtxt.Results.Where(c => c.CandidateNo.Trim() == l.CandidateNo.Trim()).ToList();

            nonMatchedData.Add(csvData);
        }

        //loop through the data we have 
        foreach(var d in nonMatchedData)
        {
            //lets make the api call
            var result = makeApiCall(d.Candidate.CandidateNo, Convert.ToInt32(d.Candidate.ExamDate), d.Candidate.ReferenceNo).Result;
            if(result == null)
            {
                continue;
            }

            //lets convert the response to an object...
            APIResults apiResults = JsonConvert.DeserializeObject<APIResults>(result);

            //lets check the status of the result, 001 means its successful
            if(apiResults.StatusCode != "001")
            {
                updateStatusOfCandidate(d.Candidate.CandidateNo, "NOT FOUND");
                continue;
            }

            //lets do the compare it self 
            foreach(var s in apiResults.Result.SubjectCol)
            {
                //lets get the subject id, then use link to obtain the results...
                int subId = getSubjectId(s.Subject);
                if(subId == 0)
                {
                    continue;
                }

                //since we have the subject id, lets check the data we have from the csv to be sure its there
                var resultCsv = d.Result.Where(c => c.SubjectID == subId).FirstOrDefault();
                //if the data is not there, we continue
                if (resultCsv == null) continue;

                //if the data exist, lets now match it with the data we have from the csv to be sure the correct grade is there
                if (resultCsv.Grade.Trim().ToLower() != s.GradeScore.Trim().ToLower())
                {
                    updateStatusOfResult(resultCsv.ResultID, "UNMATCHED");
                    //if the result do not match, lets now set the status of the result column to be unmatched...
                }
                else
                {
                    updateStatusOfResult(resultCsv.ResultID, "MATCHED");
                }
            }
            updateStatusOfCandidate(d.Candidate.CandidateNo, "COMPLETED");

        }
    }

1 个答案:

答案 0 :(得分:0)

因此,如果我正确理解,您希望Candidate表(由Result标识)中的任何CandidateNo的状态为Status "Verified"。而且,不在Candidate表中的任何Result的{​​{1}}为Status吗?

您可以在"Unverified"Candidate之间使用左联接来查找Result中但不是Candidate中的记录。

您可以在'候选'和'结果'之间使用内部联接来查找同时在ResultCandidate中的记录。

我假设ResultdbCtxt Entity Framework,例如...

DbContext