select * from db where id in (2,4,5) => id가 2, 4, 5에 해당되는 경우의 값을 뽑아라
즉 , 복수의 조건에 해당하는 데이터를 뽑아내고 싶습니다.
이를 LINQ로 구현하려면 어떻게 할까요?
설명을 위해 간단한 예시 코드를 작성하겠습니다.
1) Product 클래스 생성
![[c#] SQL in / not in 기능을 LINQ로 구현 - undefined - 1) Product 클래스 생성 클래스 생성](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
2) Product 클래스를 담을 List 생성
해당 List가 db역할을 할것임으로 db라는 이름으로 지어보았습니다.
![[c#] SQL in / not in 기능을 LINQ로 구현 - undefined - 2) Product 클래스를 담을 List 생성 [c#] SQL in / not in 기능을 LINQ로 구현 - undefined - 2) Product 클래스를 담을 List 생성](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
3) 배열 생성
![[c#] SQL in / not in 기능을 LINQ로 구현 - undefined - 3) 배열 생성 [c#] SQL in / not in 기능을 LINQ로 구현 - undefined - 3) 배열 생성](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
in (2,4,5 )에 역할을 할 배열을 하나 만듭니다.
4) 쿼리 작성
![[c#] SQL in / not in 기능을 LINQ로 구현 - undefined - 4) 쿼리 작성 [c#] SQL in / not in 기능을 LINQ로 구현 - undefined - 4) 쿼리 작성](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
배열의 Contains함수를 통해
배열에 포함되어 있는 값을 확인하면서, 배열에 있는 값만 query에 저장하는 구조입니다.
만약 not in 기능을 사용하고 싶다면, 앞에 !(느낌표)를 붙혀 주면 됩니다.
![[c#] SQL in / not in 기능을 LINQ로 구현 - undefined - 4) 쿼리 작성 [c#] SQL in / not in 기능을 LINQ로 구현 - undefined - 4) 쿼리 작성](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
'c#' 카테고리의 다른 글
[c#] localdb에 EntityFramework 연결 하는 방법 (0) | 2022.07.19 |
---|---|
[c#] LINQ First vs Single | FirstOrDefault vs SingleOrDefault 차이 (0) | 2022.07.15 |
[c#] 양수일때 + 넣기 (using String.Format) (0) | 2022.07.14 |
[c#] string format ### 000 차이 (0) | 2022.07.13 |
[c#] EventHandler란? (0) | 2022.07.12 |
댓글