본문 바로가기
카테고리 없음

c# dapper 쿼리결과를 liat dynamivc 으로 받았는데 처음 필드에 index 추가하는방법

by keisoft 2025. 5. 23.

var indexedResult = result
    .Select((item, index) =>
    {
        IDictionary<string, object> dict = (IDictionary<string, object>)new ExpandoObject();

        foreach (var prop in (IDictionary<string, object>)item)
        {
            dict[prop.Key] = prop.Value;
        }

        dict["Index"] = index + 1; // 1부터 시작하는 인덱스

        return (dynamic)dict;
    })
    .ToList();