1.I have a result object with 100 data records from db.Based on diffrent if condition i have to store that records in a list.I am adding every records in their satisfied condition loop.BUt when I see the final object all 100 records are same.What will be the issue?
2. is there any refatoed method for this same logic as i am written some entry level code pls suggest
My method:
foreach (var data in MainObj) { if (!String.IsNullOrEmpty(data.RealKey)) { if (data.ResultStatus == "Failed") { FailedTestCases.Add(data.TestCases); }
if (FailedTestCases.Count > 0) { alltcPassed = false; } else { alltcPassed = true; }
if (isValidKey && alltcPassed) { ResultSummary.TestCases = JsonConvert.SerializeObject(FailedTestCases); ResultSummary.TestType = data.TestType; ResultSummary.ResultStatus = data.ResultStatus; result.Add(ResultSummary);
} if (isValidKey && !alltcPassed) { ResultSummary.TestCases = JsonConvert.SerializeObject(FailedTestCases); ResultSummary.TestType = data.TestType; ResultSummary.ResultStatus = data.ResultStatus; result.Add(ResultSummary);
} if (!isValidKey && alltcPassed) { ResultSummary.TestCases = JsonConvert.SerializeObject(FailedTestCases); ResultSummary.TestType = data.TestType; ResultSummary.ResultStatus = data.ResultStatus; result.Add(ResultSummary); }
if (!isValidKey && !alltcPassed) { ResultSummary.TestCases = JsonConvert.SerializeObject(FailedTestCases); ResultSummary.TestType = data.TestType; ResultSummary.ResultStatus = data.ResultStatus; result.Add(ResultSummary); } }
// this else happen when this condition true the firstone foreach(!String.IsNullOrEmpty(data.RealKey))
else { if (data.ResultStatus == "Failed") { FailedTestCases.Add(data.TestCases);
ResultSummary.TestCases = JsonConvert.SerializeObject(FailedTestCases); ResultSummary.TestType = data.TestType; ResultSummary.ResultStatus = data.ResultStatus; result.Add(ResultSummary);
} if (data.ResultStatus == "Passed" && FailedTestCases.Count==0) { ResultSummary.TestCases = data.TestCases; ResultSummary.TestType = data.TestType; ResultSummary.ResultStatus = data.ResultStatus; result.Add(ResultSummary);
} } } return Task.FromResult(result.FirstOrDefault()); //here result having 100 datas but all are same need to get 100 diffrent record }
PLEASE SUGGEST ANY MODERN OR REFACTORED CODE AS I HAVE WRITTEN AN ENTRY LEVEL CODE FOR THIS LOGIC