rajesh yadav

rajesh yadav

  • 1.7k
  • 82
  • 9.9k

following is my api , is enumerable i s better than list etc

Feb 27 2020 11:07 PM
  1. public class SCurve  
  2. {  
  3. public DateTime ProgressDate { getset; }  
  4. public decimal PlannedProgressPercentageWithWeightage { getset; }  
  5. public decimal ActualProgressPercentage { getset; }  
  6. public decimal PlannedPrgressPercentageWithWeightageCumulative { getset; }  
  7. public decimal ActualProgressPercentageCumulative { getset; }  
  8. }  
following is what i get from database. in tuples/record. 
  1. "nodeHierarchy": [{ "L1""Mechanical" }, { "L2""nical" }, { "L3""nical2" }], "weightage""34""dateFrom""01 jan 2020""dateTo""04 Feb 2020""WSID""Rky6""scope""4234""IsLeafNode"true"Parentnode": [{ "L1""Mechanical" }, { "L2""nical" }] }  
  2.   
  3. "nodeHierarchy": [{ "L1""Mechanical" }, { "L2""nical" }], "weightage""34""dateFrom""01 jan 2020""dateTo""04 Feb 2020""WSID""Rky6""scope""4234""IsLeafNode"true"Parentnode": [{ "L1""Mechanical" }] }  
  4.   
  5. "nodeHierarchy": [{ "L1""Mechanical" ], "weightage""34""dateFrom""01 jan 2020""dateTo""04 Feb 2020""WSID""Rky6""scope""4234""IsLeafNode"true"Parentnode": [] }  
  1. [HttpGet]  
  2. public IEnumerable GetSCurvePlan(string WSId, string dtFrom, string dtTo, string ReportType)  
  3. {  
  4. //I get json from databse given above then transfrom it in following list.  
  5. List lstSCurveObj = new List();  
  6. IEnumerable IenumObject = lstSCurveObj.GroupBy(val => val.ProgressDate.AddDays(((CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek) - val.ProgressDate.DayOfWeek) + 6))  
  7. .Select(grouped => new  
  8. {  
  9. ProgressDate = grouped.Key,  
  10. PlannedProgressPercentageWithWeightage = grouped.Sum(Pp => Pp.PlannedProgressPercentageWithWeightage),  
  11. ActualProgressPercentage = grouped.Sum(Ap => Ap.ActualProgressPercentage),  
  12. grouped.OrderByDescending(Pc => Pc.ProgressDate).First().PlannedPrgressPercentageWithWeightageCumulative,  
  13. grouped.OrderByDescending(Ac => Ac.ProgressDate).First().ActualProgressPercentageCumulative  
  14. }).OrderBy(o=> o.ProgressDate).ToList();  
  15. return IenumObject;  
  16. }  
Q1) I have to return the data of the class in json format, so u can change anything if u want. my objective is that it should be optimised?
 
q2) is tolist() function is required, because, without it, it works? for optimisation.
 
as there is no where clause and it is used only once.
 
Q3) initially I was returning List but it was showing IEnumerable anonymous type cant be cast to List
so i converted it to ienumerbal how far it is good or there is other better ways?
 
Q4) as i have used orderby and tolist , then i have returened , will it keep the order or i have to write like following
 
return IenumObject.OrderBy(o => o.ProgressDate)
 
yours sincerely

Answers (3)