문제

나는 List<int> 그리고 List<customObject>.customObject 클래스에는 ID 속성이 있습니다.어떻게 얻을 수 있나요? List<customObject> ID 속성이 있는 객체만 포함합니다. List<int> LINQ를 사용하시나요?

편집하다:읽기가 더 쉽고 직관적이기 때문에 Konrad의 답변을 수락했습니다.

도움이 되었습니까?

해결책

var result = from o in objList where intList.Contains(o.ID) select o

다른 팁

using System.Linq;

objList.Where(x => intList.Contains(x.id));

나는 지금 비슷한 문제가 있었고 아래 해결 방법을 사용했습니다.객체 목록이 이미 있는 경우 int 목록에 없는 항목을 모두 제거하고 objList에 일치하는 항목만 남겨 둘 수 있습니다.

objList.RemoveAll(x => !intList.Contains(x.id));

테스트되지 않았지만 다음과 같습니다.

var matches = from o in objList 
                  join i in intList on o.ID equals i
                  select o;

@Konrad가 방금 테스트했는데 제대로 작동합니다. "i" 대신 "i.ID"를 쓴 곳에 오타가 있었습니다.

완전성을 위해 (아마 읽기 더 쉬울 수도 있겠죠?) Matt의 "join"과 유사한 "where"를 사용합니다.

var matches = from o in customObjectList
              from i in intList
              where o.ID == i
              select o;

목록의 항목 수가 49개를 초과하면 포함 대신 조인을 사용하면 작동하지 않습니다.다음과 같은 오류가 발생합니다. Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top