LINQ를 배우고 있으며 다음 코드와 동일한 LINQ 쿼리를 원합니다.C# LINQ TimeSpan 평균 일수
IEnumerable 목록에는 정렬 된 날짜 목록이 포함됩니다 : 오래된 항목부터 최신 항목까지.
아래 코드는 배열의 첫 번째 요소 날짜, 두 번째 요소 날짜의 첫 번째 요소 날짜, 세 번째 요소의 두 번째 요소 날짜 등을 빼서 TimeSpan
을 생성합니다. 그런 다음 TimeSpan.Days
은 코드의 다른 곳에서 평균됩니다.
LINQ 쿼리가 배열 생성을 필요로하지 않는다고 믿습니다. IEnumerable<DateTime>
구조를 데이터 소스로 사용할 수 있습니다.
IEnumerable<DateTime> list; // contains a sorted list of oldest to newest dates
// build DateTime array
DateTime[] datesArray = null;
TimeSpan ts;
List<int> daysList = new List<int>();
datesArray = (from dt in list
select dt).ToArray();
// loop through the array and derive the TimeSpan by subtracting the previous date,
// contained in the previous array element, from the date in the current array element.
// start the loop at element 1.
for (int i = 1; i < list.Count(); i++)
{
ts = datesArray[i].Subtract(datesArray[i - 1]); // ts is a TimeSpan type
daysList.Add(ts.Days);
// add the TimeSpan.Days to a List<int> for averaging elsewhere.
}
내가 원하는 생각
스콧
'(목록에서 DT에서하는 DT를 선택) .ToArray()'더 나은로 기록 된'list.ToArray()가' – Gabe