티스토리 뷰

DB를 한번 다녀온 리스트 컬렉션에 대해서

LINQ(링큐) 문을 이용해서 다시 재가공 하는 방법


일반적으로 아는 방법으로 해도 상관없으나

다시 리스트형식이나 ToArray() 방식으로 받으려면

anonymous types (익명형식에 맞지 않습니다) 라는 에러가 발생하거나, 그외 잡다한 익명타입 관련 에러가 발생하는 경우가 있다. 아래 방법1, 2처럼 사용하면 LINQ 에서도 LIST나 다른 Collection 객체로 받을 수 있다.


    public class Organizational

    { 

        public string DeptLv1_Code { get; set; }

        public string DeptLv1_Name { get; set; } 

        public string DeptLv2_Code { get; set; }

        public string DeptLv2_Name { get; set; } 

        public string DeptLv3_Code { get; set; }

        public string DeptLv3_Name { get; set; } 

        public string Person_Code { get; set; }

        public string Person_Name { get; set; }

        public string Person_Photo { get; set; } 

        public int Disp_Seq { get; set; }

    }


방법1.

IEnumerable<Organizational> orgItem = from p in OrgList

                                          where p.DeptLv3_Code == cntPolitc[i]

                                           orderby p.Disp_Seq

                                           select new Organizational { DeptLv1_Code = p.DeptLv1_Code, DeptLv1_Name = p.DeptLv1_Name, DeptLv2_Code = p.DeptLv2_Code, DeptLv2_Name = p.DeptLv2_Name, DeptLv3_Code = p.DeptLv3_Code, DeptLv3_Name = p.DeptLv3_Name, Disp_Seq = p.Disp_Seq, Person_Code = p.Person_Code, Person_Name = p.Person_Name, Person_Photo = p.Person_Photo };



방법2

List<Organizational> orgItem = (from p in OrgList

                                          where p.DeptLv3_Code == cntPolitc[i]

                                           orderby p.Disp_Seq

                                           select new Organizational { DeptLv1_Code = p.DeptLv1_Code, DeptLv1_Name = p.DeptLv1_Name, DeptLv2_Code = p.DeptLv2_Code, DeptLv2_Name = p.DeptLv2_Name, DeptLv3_Code = p.DeptLv3_Code, DeptLv3_Name = p.DeptLv3_Name, Disp_Seq = p.Disp_Seq, Person_Code = p.Person_Code, Person_Name = p.Person_Name, Person_Photo = p.Person_Photo }).ToList();

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함